有什么区别?:,?!和?=在正则表达式? [英] what is the difference between ?:, ?! and ?= in regex?
问题描述
我搜索了这些表达式的含义,但无法理解它们之间的确切差异。
这就是他们所说的:
I searched for the meaning of these expressions but couldn't understand the exact differnce between them. This is what they say:
-
?:
匹配表达但是不要捕获它。 -
?=
匹配后缀但将其从捕获中排除。 -
?!
如果没有后缀,则匹配。
?:
Match expression but do not capture it.?=
Match a suffix but exclude it from capture.?!
Match if suffix is absent.
我尝试过使用这些简单的RegEx并获得了类似的结果。
示例:以下3个表达式给出非常相似的结果。
I tried using these in simple RegEx and got similar results for all. example: the following 3 expressions give very similar results.
-
[a-zA-Z0- 9 ._-] + @ [a-zA-Z0-9 - ] +(?!\。[a-zA-Z0-9] +)*
-
[a-zA-Z0-9 ._-] + @ [a-zA-Z0-9 - ] +(?= \。[a-zA-Z0-9] +)*
-
[a-zA-Z0-9 ._-] + @ [a-zA-Z0-9- ] +(?:\。[a-zA-Z0-9] +)*
[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(?!\.[a-zA-Z0-9]+)*
[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(?=\.[a-zA-Z0-9]+)*
[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9]+)*
推荐答案
?=
和之间的区别?!
是前者需要的给定表达式匹配,后者要求不匹配。例如 a(?= b)
将匹配ab中的a,但不匹配ac中的a。而 a(?!b)
将匹配ac中的a,而不匹配ab中的a。
The difference between ?=
and ?!
is that the former requires the given expression to match and the latter requires it to not match. For example a(?=b)
will match the "a" in "ab", but not the "a" in "ac". Whereas a(?!b)
will match the "a" in "ac", but not the "a" in "ab".
?:
和?=
之间的区别是? =
排除整个比赛中的表达式,而?:
只是不创建捕获组。所以例如 a(?:b)
将匹配abc中的ab,而 a(?= b)
只会匹配abc中的a。 a(b)
将匹配abc中的ab和创建包含b的捕获。
The difference between ?:
and ?=
is that ?=
excludes the expression from the entire match while ?:
just doesn't create a capturing group. So for example a(?:b)
will match the "ab" in "abc", while a(?=b)
will only match the "a" in "abc". a(b)
would match the "ab" in "abc" and create a capture containing the "b".
这篇关于有什么区别?:,?!和?=在正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!