[英]How do I force regex to match the 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=""
匹配后我们有: a =“想要匹配最长”,b =“”
but i want :a="" , b="longest"
但我想: a =“”,b =“最长”
RegEx is "greedy" by default, meaning it will match as much as possible . RegEx默认为“贪婪”,这意味着它将尽可能匹配。 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). 现在这将懒惰地匹配0+
[\\w\\W]
字符,或换句话说:直到表达式可以继续匹配(一旦它看到want to match longest available
等等)。
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
^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.