我有一堆5个字母的字符串。对于每个字符串,我只想在该字符串包含3个相同字母的实例时进行匹配,即:
Case 1: 'aabbc' -> no match
Case 2: 'bbbcc' -> match 'bbb'
Case 3: 'ddcdc' -> match 'ddd'
我最好的正则表达式尝试是:
(.){1}(?!\1)*\1{1}(?!\1)*\1{1}
这适用于情况1(无匹配项)和情况2(3个实例相邻),但不适用于情况3(3个实例由至少一个其他字母分隔)。是否存在适用于情况3的正则表达式?理想情况下,我还要从字符串中提取3个匹配实例的位置。
您可以使用以下正则表达式来确定一个字符是否出现至少3次。
Demo
This does not check that the characters are letters but it does work with any characters. To restrict to letters change each
.
to[a-z]
or[a-zA-Z]
, as appropriate.I think the pattern
([a-z]).*?\1.*?\1
does what you want, although there are likely to be edge cases that would complicate it.该模式将查找三个小写字母,并且它们之间包含0个或多个字符。
然后,您可以仅提取捕获组以获取您的匹配位置。
At the moment, the pattern only looks for any lowercase letter repeated three times, but you could change the initial capturing group -
([a-z])
- if you wanted to capture something else.