How to make Regular expression into non-greedy?
Simply turn a greedy regEx into a non-greedy one by adding a ?
after quantifiers like *
or +
. This ensures the engine will find the shortest match.
Example:
- Greedy:
/.*/
- Non-greedy:
/.*?/
Code snippet:
Journey from greedy to non-greedy
When working with regular expressions, the difference between greedy and non-greedy (or lazy) matches are of significant importance. A greedy match aims to consume as much of the input as possible, while a non-greedy match is designed to do the opposite.
Non-greedy regex: The superheroes of extraction
Non-greedy expressions come into their own when parsing through text containing special character blocks or frequent separators. Such quantifiers tell the match to stop at the first instance of the next valid pattern. This efficiently prevents overmatching and ensures the extraction of multiple results from a single string.
When to lean on non-greedy expressions
- Parsing HTML/XML tags, where nesting is common
- Extracting content within parentheses or brackets
- Data where start and end delimiters are repeated characters
Easing the job: Negated character classes and possessive quantifiers
In addition to transforming regex to non-greedy with *?
, you can use negated character classes (e.g., [^)]*
matches everything but a closing parenthesis) and possessive quantifiers (e.g, *+
), which discourage backtracking, thus increasing efficiency.
The "g" in regex: searching globally
Utilize the global (g
) flag to ensure you find all non-greedy matches, not just the first one:
Nudging with "{n,m}" construct in regex
Need more control? Modify {n,m}
with ?
for specialised non-greedy matching:
Here, the greedy \d{2,5}
attempts to match2 to 5 digits max, whereas the non-greedy \d{2,5}?
is content with the smallest valid match, i.e., 2 digits.
Leaning on Possessive Quantifiers
For matching scenarios where you don't want any backtracking, possessive quantifiers like ++
or *+
come to the rescue:
The possessive \d*+!
fails to match because it's too possessive and won't let go of the digits
to match !
.
Was this article helpful?