找到某个符号之间的重复项

时间:2015-11-14 19:17:58

标签: regex

是否可以检测到字符串中有4个相同的字母由其他字符分隔,如下所示:abb*bbcbb*bb)或a*aaa?我尝试(\S+\s*)\1,它适用于abb*bbc,但它也会找到bbabbcd

1 个答案:

答案 0 :(得分:1)

(稍微丑陋)正则表达式的解决方案是:

([a-z])(?:\*\1\1|\1\*\1|\1\1\*)\1

那是:

([a-z])    any letter
(?:        one of the following:
  \*\1\1|   - a *, and then the letter twice
  \1\*\1|   - letter, *, letter
  \1\1\*    - letter, letter, *
)
\1         the fourth letter