正则表达式懒惰的量词表现出贪婪 [英] Regex lazy quantifier behave greedy
问题描述
我有这样的文字;
[Some Text][1][Some Text][2][Some Text][3][Some Text][4]
我想将[Some Text][2]
与此正则表达式匹配;
I want to match [Some Text][2]
with this regex;
/\[.*?\]\[2\]/
但是它返回[Some Text][1][Some Text][2]
我怎么只能匹配[Some Text][2]
?
注意:某些文本中可以包含任何字符,包括[
和]
,并且方括号中的数字可以是不仅是1和2的任何数字.我要匹配的Some Text
可以位于行的开头,可以有多个Some Text
Note : There can be any character in Some Text including [
and ]
And the numbers in square brackets can be any number not only 1 and 2. The Some Text
that i want to match can be at the beginning of the line and there can be multiple Some Text
s
推荐答案
\[.*?\]\[2\]
模式工作原理这个:
-
\[
-查找最左边的[
(正则表达式引擎处理从左到右的字符串输入) -
.*?
-匹配除换行符以外的任何0+字符,尽可能少,但为成功匹配所需要的尽可能多,还有后续的模式,请参见下文 -
\]\[2\]
-][2]
子字符串.
\[
- finds the leftmost[
(as the regex engine processes the string input from left to right).*?
- matches any 0+ chars other than line break chars, as few as possible, but as many as needed for a successful match, as there are subsequent patterns, see below\]\[2\]
-][2]
substring.
因此,.*?
在每次失败时都会扩展,直到找到最左边的][2]
. 请注意,懒惰的量词不能保证最短"匹配..
So, the .*?
gets expanded upon each failure until it finds the leftmost ][2]
. Note the lazy quantifiers do not guarantee the "shortest" matches.
解决方案
使用否定字符类(而不是边界char)来代替.*?
(或.*
),而使用否定字符类.
Instead of a .*?
(or .*
) use negated character classes that match any char but the boundary char.
\[[^\]\[]*\]\[2\]
请参见此regex演示.
在这里,.*?
替换为[^\]\[]*
-除]
和[
之外的0个或更多字符.
Here, .*?
is replaced with [^\]\[]*
- 0 or more chars other than ]
and [
.
其他示例:
-
<[^<>]*>
匹配内部没有<
和>
的<...>
-
\([^()]*\)
匹配内部没有(
和)
的(...)
-
"[^"]*"
匹配内部没有"
的"..."
<[^<>]*>
matches<...>
with no<
and>
inside\([^()]*\)
matches(...)
with no(
and)
inside"[^"]*"
matches"..."
with no"
inside
在其他情况下,当起始模式是多字符字符串或复杂模式时,请使用调和贪婪令牌,(?:(?!start).)*?
.要匹配abc 0 abc 1 def
中的abc 1 def
,请使用abc(?:(?!abc).)*?def
.
In other situations, when the starting pattern is a multichar string or complex pattern, use a tempered greedy token, (?:(?!start).)*?
. To match abc 1 def
in abc 0 abc 1 def
, use abc(?:(?!abc).)*?def
.
这篇关于正则表达式懒惰的量词表现出贪婪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!