Pattern : "I (?<a>[\w\W]*)(want to match (?<b>longest))? available"
or "I ((?<a>[\w\W]*)|(want to match (?<b>longest))?)+ available"
String :
after match we have :
but i want :
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
^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.