我正在尝试检查一个单词是否有三个或更多重复的“两个字母配对”,但只有当重复相同 字母时,表达式才会返回 true。为什么?
(([a-z])([^\1]))\1\2{2,}
^ ^ ^ ^ ^
1 2 3 4 5
1) 任意字母(捕获集\1)
2) 任何未设置 1 的字符(捕获集\2)
3) 再次捕获\1
4) 再次捕获\2
5) 至少两次
应该返回 TRUE 的词: asasasassafasf, ereeeerererere, dddddtrtrtrtruuuuuuuu
应该返回 FALSE 的词: dddddddd,rrrrrrrrrrrrlkajf,fffffffssssssytytfffffff
最佳答案
您可以使用 negative lookahead assertion 解决此问题:
([a-z])((?!\1)[a-z])(?:\1\2){2,}
测试一下 live on regex101.com .
解释:
([a-z]) # Match and capture a single ASCII letter in group 1
( # Match and capture...
(?!\1) # unless it's the same letter as the one captured before
[a-z] # a single ASCII letter in group 2
) # End of group 2
(?:\1\2) # Match those two letters
{2,} # two or more times
关于php - 正则表达式捕获 : repeating two letters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840353/