简体   繁体   English

如何强制正则表达式匹配模式的最长部分。

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

I have a pattern in .net and I want a string be matched with longest possible part of the pattern 我在.net中有一个模式,我想要一个字符串与模式的最长部分匹配

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM