您会混淆非捕获组
(?:...)和超前断言
(?=...)。
前者确实参加了比赛(因此其中一部分
match.group()包含了整体比赛),他们只是不生成反向引用(
$1等以备后用)。
第二个问题(为什么双点匹配?)比较棘手。这是由于您的正则表达式错误。你看,当你写的时候(简明扼要)
[+-/]
您写了“在
+和之间匹配一个字符
/,并且在ASCII中,点在它们之间(ASCII
43-47:)
+,-./。因此,第一个字符类与该点匹配,并且永远不会达到先行断言。您需要放置字符类末尾的破折号将其视为文字破折号:
((w # alphanumeric and _| [!#$%&'*+/=?^_`{|}~-] # special chars, but no dot at beginning)(w # alphanumeric and _| [!#$%&'*+/=?^_`{|}~-] # special characters| ([.](?![.])) # negative lookahead to avoid pairs of dots. )*)(?<!.)(?=@)# no end with dot before @当然,如果您想使用此逻辑,可以对其进行简化:
^(?!.) # no dot at the beginning(?:[w!#$%&'*+/=?^_`{|}~-] # alnums or special characters except dot| (.(?![.@])) # or dot unless it's before a dot or @ )*(?=@) # end before @


