?:,?!之间的区别和?= [英] Difference between ?:, ?! and ?=
问题描述
我搜索了这些表达式的含义,但无法理解它们之间的确切区别.
I searched for the meaning of these expressions but couldn't understand the exact difference between them.
他们是这样说的:
-
?:
匹配表达式,但不捕获它. -
?=
匹配后缀,但将其从捕获中排除. -
?!
如果没有后缀,则匹配.
?:
Match expression but do not capture it.?=
Match a suffix but exclude it from capture.?!
Match if the suffix is absent.
我尝试在简单的RegEx中使用它们,并获得了相似的结果.
I tried using these in simple RegEx and got similar results for all.
例如:以下3个表达式给出的结果非常相似.
For 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屋!