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"
RegEx is "greedy" by default, meaning it will match as much as possible. To make a repetition lazy, add a
?
.This will now match 0+
[\w\W]
characters lazily, or in other words: until the expression can continue to match (once it seeswant 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: