php - 正则表达式捕获 : repeating two letters

标签 php regex

我正在尝试检查一个单词是否有三个或更多重复的“两个字母配对”,但只有当重复相同 字母时,表达式才会返回 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/

相关文章:

.net - 文本提要的正则表达式替换

php - 需要时自动加载函数/类库

php - Elasticsearch - 我需要 JDBC 驱动程序吗?

regex - 使用 RegEx 识别数据库连接字符串的各个部分

javascript - 如何匹配字符,但如果它们严格用引号引起来则不匹配

c++ - 从源代码中删除 C++ 注释

javascript - jquery 的 find 方法不适用于 html 字符串来提取一部分 - Jquery

php - 如何从 Facebook 登录获取用户详细信息?

javascript 包含在内,因为 php 不工作

javascript - Symfony2 和 Angular.JS 之间共享的翻译