What does (?<=x)
mean in regex?
在regex中(?<=x)是什么意思?
By the way, I have read the manual here.
顺便说一下,我在这里读过手册。
5 个解决方案
#1
10
It's a positive lookbehind.
这是一个积极的向后插入。
(?<=a)b
(positive lookbehind) matches theb
(and only theb
) incab
, but does not matchbed
ordebt
.(?<=a)b (positive lookbehind)匹配出租车内的b(只有b),但不匹配床或债务。
You won't find it in any JavaScript manual because it's not supported in JavaScript regex:
您不会在任何JavaScript手册中找到它,因为JavaScript regex不支持它:
Finally, flavors like JavaScript, Ruby and Tcl do not support lookbehind at all, even though they do support lookahead.
最后,JavaScript、Ruby和Tcl这样的风格根本不支持lookbehind,尽管它们确实支持lookahead。
#2
2
From the Python re
documentation:
来自Python的re文档:
(?<=...)
(? < =…)
Matches if the current position in the string is preceded by a match for
...
that ends at the current position. This is called a positive lookbehind assertion.(?<=abc)def
will find a match inabcdef
, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning thatabc
ora|b
are allowed, buta*
anda{3,4}
are not. Note that patterns which start with positive lookbehind assertions will never match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:如果字符串中的当前位置前面有一个匹配为…它在当前位置结束。这被称为“积极的向后断言”。def将在abcdef中找到一个匹配项,因为lookbehind将备份3个字符并检查所包含的模式是否匹配。所包含的模式必须只匹配某个固定长度的字符串,这意味着允许使用abc或|b,但不允许使用a*和a{3,4}。注意,以积极的lookbehind断言开头的模式永远不会在搜索字符串的开头匹配;您很可能希望使用search()函数,而不是match()函数:
>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
This example looks for a word following a hyphen:
这个例子查找连字符后面的单词:
>>> m = re.search('(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'
#3
2
It's called a positive look behind, it's looking backwards for the character x, note this isn't supported by javascript though. For future reference, here's a better manual :)
它被称为积极的向后看,它向后看字符x,注意这不是由javascript支持的。为了以后的参考,这里有一个更好的手册:)
#4
1
From regular-expressions.info:
从regular-expressions.info:
Zero-width positive lookbehind. Matches at a position if the pattern inside the lookahead can be matched ending at that position (i.e. to the left of that position). Depending on the regex flavor you're using, you may not be able to use quantifiers and/or alternation inside lookbehind.
任意正向后插入。匹配在一个位置,如果在前视中的模式可以匹配结束在那个位置(即在那个位置的左边)。根据您使用的regex味道,您可能无法使用量词和/或在lookbehind中进行修改。
#1
10
It's a positive lookbehind.
这是一个积极的向后插入。
(?<=a)b
(positive lookbehind) matches theb
(and only theb
) incab
, but does not matchbed
ordebt
.(?<=a)b (positive lookbehind)匹配出租车内的b(只有b),但不匹配床或债务。
You won't find it in any JavaScript manual because it's not supported in JavaScript regex:
您不会在任何JavaScript手册中找到它,因为JavaScript regex不支持它:
Finally, flavors like JavaScript, Ruby and Tcl do not support lookbehind at all, even though they do support lookahead.
最后,JavaScript、Ruby和Tcl这样的风格根本不支持lookbehind,尽管它们确实支持lookahead。
#2
2
From the Python re
documentation:
来自Python的re文档:
(?<=...)
(? < =…)
Matches if the current position in the string is preceded by a match for
...
that ends at the current position. This is called a positive lookbehind assertion.(?<=abc)def
will find a match inabcdef
, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning thatabc
ora|b
are allowed, buta*
anda{3,4}
are not. Note that patterns which start with positive lookbehind assertions will never match at the beginning of the string being searched; you will most likely want to use the search() function rather than the match() function:如果字符串中的当前位置前面有一个匹配为…它在当前位置结束。这被称为“积极的向后断言”。def将在abcdef中找到一个匹配项,因为lookbehind将备份3个字符并检查所包含的模式是否匹配。所包含的模式必须只匹配某个固定长度的字符串,这意味着允许使用abc或|b,但不允许使用a*和a{3,4}。注意,以积极的lookbehind断言开头的模式永远不会在搜索字符串的开头匹配;您很可能希望使用search()函数,而不是match()函数:
>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'
This example looks for a word following a hyphen:
这个例子查找连字符后面的单词:
>>> m = re.search('(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'
#3
2
It's called a positive look behind, it's looking backwards for the character x, note this isn't supported by javascript though. For future reference, here's a better manual :)
它被称为积极的向后看,它向后看字符x,注意这不是由javascript支持的。为了以后的参考,这里有一个更好的手册:)
#4
1
From regular-expressions.info:
从regular-expressions.info:
Zero-width positive lookbehind. Matches at a position if the pattern inside the lookahead can be matched ending at that position (i.e. to the left of that position). Depending on the regex flavor you're using, you may not be able to use quantifiers and/or alternation inside lookbehind.
任意正向后插入。匹配在一个位置,如果在前视中的模式可以匹配结束在那个位置(即在那个位置的左边)。根据您使用的regex味道,您可能无法使用量词和/或在lookbehind中进行修改。