Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change my regex, so it will not match extra junk at the end?

I have a text like that:

asd dsa [SKU].[Analytic5].&[omg wtf] not found blah blah blah "&[omg wtf]".  Query Text: <ccon>SELECT {[SKU].[Analytic5].&[omg wtf]} ON 0 FROM [Model_week] CELL PROPERTIES CELL_ORDINAL</ccon>

I need to extract [SKU].[Analytic5].&[omg wtf] from there. Actually it’s [SKU].[*].&[*].

I made this regex:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

\[SKU\]\.\[.*\]\.\&\[.*\]

It works… kind of… it grabs all the junk after first [omg wtf] until the final [Model_week] met.
So the result of my regex is:

[SKU].[Analytic5].&[omg wtf] not found blah blah blah "&[omg wtf]".  Query Text: <ccon>SELECT {[SKU].[Analytic5].&[omg wtf]} ON 0 FROM [Model_week]

I just can’t understand how to limit it. I tried things like putting {1}, but it doesn’t help.

>Solution :

The RegEx quantifier * is a greedy quantifier with a lazy equivalent *?.

The difference between greedy and lazy quantifiers is that the greedy quantifier will try to match as many characters as possible whereas lazy quantifier will try to match least characters possible.

The effect of this in your example is that [.*\] matches everything from the first [ to the last ]. But if you change those quantifiers to their lazy equivalents your RegEx will work as you intended.

The example below with lazy quantifiers will match what you expect it to match:

\[SKU\]\.\[.*?\]\.\&\[.*?\]

Output of the RegEx above when used on your snippet:

[SKU].[Analytic5].&[omg wtf]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading