3

我正在使用正则表达式(\b\w+\b)\W+\1{3,}过滤字符串重复三次或更多次的 url。我试过了(\b\w+\b)\W+\1{3,}(\b\w+\b)\W{3,}+\1但没有帮助

http://rubular.com/r/6IyCPyBiuW -> (\b\w+\b)\W+\1-> 这可以找到重复超过一次的单词,但我有兴趣找到重复超过三次的单词。

http://rubular.com/r/O9NcobUsTX -> (\b\w+\b)\W+\1{3,}-> 这不能找到重复三个或更多的单词

4

1 回答 1

5

以下正则表达式有效:

(\w+\W)\1{2,}

上面的内容也与非单词字符完全匹配,因此,或者,您可以使用相当丑陋的外观

(\w+)(?:\W+\1){2,}

细节:

\w    -> single word character
\w+   -> one or more word characters
\W    -> non-word character
\1    -> back-reference to capturing group #1 (in this case, (\w+)
{2,}  -> 2 or more of (?:\W+\1)
(?:)  -> grouping without actually capturing anything

http://rubular.com/r/Trb41xxCAt

于 2013-03-18T15:20:34.170 回答