How do I force regex to match the longest possible part of the pattern.

17.1k views Asked by At

I have a pattern in .net and I want a string be matched with longest possible part of the pattern

Pattern : "I (?<a>[\w\W]*)(want to match (?<b>longest))? available"
or "I ((?<a>[\w\W]*)|(want to match (?<b>longest))?)+ available"

String : "I want to match longest available"

after match we have : a="want to match longest" , b=""
but i want : a="" , b="longest"

1

There are 1 answers

2
Sam On

RegEx is "greedy" by default, meaning it will match as much as possible. To make a repetition lazy, add a ?.

I <?a:[\w\W]*?>(want to match <?b:longest>)? available
             ^

This will now match 0+ [\w\W] characters lazily, or in other words: until the expression can continue to match (once it sees want to match longest available, etc).

Examples: greedy vs. lazy (click 'regex debugger' to see how each of these repetitions operates).

Same idea goes with your other expression, however the greediness is a problem in a different location:

I ((?<a>[\w\W]*)|(want to match (?<b>longest))?)+? available
                                                 ^