正则表达式:最小可能匹配或非贪婪匹配
如何告诉 RegEx(.NET 版本)获取最小的有效匹配而不是最大的有效匹配?
How do I tell RegEx (.NET version) to get the smallest valid match instead of the largest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于
.*
或.+
等正则表达式,请附加问号(.*?
或.+?
) 匹配尽可能少的字符。要选择性地匹配(?:blah)?
部分,但除非绝对必要,否则不匹配,请使用类似(?:blah){0,1}?
的内容。对于重复匹配(使用{n,}
或{n,m}
语法)附加问号以尝试匹配尽可能少的内容(例如{3,}?
或{5,7}?
)。关于正则表达式量词的文档也可能乐于助人。
For a regular expression like
.*
or.+
, append a question mark (.*?
or.+?
) to match as few characters as possible. To optionally match a section(?:blah)?
but without matching unless absolutely necessary, use something like(?:blah){0,1}?
. For a repeating match (either using{n,}
or{n,m}
syntax) append a question mark to try to match as few as possible (e.g.{3,}?
or{5,7}?
).The documentation on regular expression quantifiers may also be helpful.
非贪婪运算符
?
。就像这样:The non-greedy operator,
?
. Like so:非贪婪运算符并不意味着最短的可能匹配;例如,在字符串上
...
a.+?k
将匹配整个字符串(在本例中),而不是仅匹配最后三个符号。我实际上想找到最小的可能匹配。
这是 '
a
' 的最后一个可能的匹配,但仍然允许k
的所有匹配。对于单字符起始序列,一种方法是使用如下表达式:
The non-greedy operator does not mean the shortest possible match; for example, on string
…
a.+?k
will match the entire string (in this example) instead of only the last three signs.I'd like to actually find the smallest possible match instead.
That is that last possible match for '
a
' to still allow all matches fork
.For single-character start sequences, one way to do that is to make use of an expression like:
负向前瞻在这里会有所帮助
示例:
a 和 b 可以更大
注意:这不会找到字符串中最短的匹配。
这仍然找到
a..b
而不是ab
,因此它不是“最小可能匹配”。我不确定您是否可以使用正则表达式找到尽可能小的匹配项。您可以找到所有匹配项,然后在这些结果中找到最小的。A negative lookahead would help here
Example:
a and b can be larger
Note: this won't find the shortest match in the string.
This still finds
a..b
nota.b
so it's not "Smallest possible match". I'm not sure you can find smallest possible match with regex. You could find all matches and then in those results find the smallest.