regexregex-greedymicrosoft-expression-web

Confusion regarding the *? regular expression operator


So I want to search a string, using the below regular expression:

border-.*\.5pt

to find all border-top, border-bottom, etc CSS properties in a file with a border thickness of .5pt. It generally works great, but it's too greedy.

For example all of the below comes back as a single match:

border-top:solid #1F497D .5pt;border-bottom:solid #1F497D .5pt

I want those two CSS properties to be two separate matches.

So I tried to modify my regular expression to:

border-.*?\.5pt

Using ? to make it non-greedy. However, after that modification, nothing matches.

Can anyone explain why I see this behavior? What am I missing?

(If it's worth knowing, I'm using Microsoft Expression Web's 'find with regular expressions' when doing this search.)


Solution

  • There is no one "regular expression" language. While there are broad commonalities, details differ from implementation to implementation. Many regexes use - to be the non-greedy "0 or more", others use *?. Apparently Microsoft Expression Web uses @.

    In short, regexes can differ, so you'll often need to RTM for the one you're using to find its range of capabilities and detailed syntax (i.e. support for alteration/backtracking/etc., grouping character, set shorthand, etc.)