PHP 获取字符串中重复字符的单词的正则表达式

PHP Regular expression to get words with repeated chars in a string

我正在尝试获取包含重复字符的字符串中的单词。 例如:"II loooovve this video. It's awesooooommeee."

我怎样才能得到结果: 喜欢 太棒了 ?

您可以将此正则表达式与反向引用一起使用:

\b\w*(\w)\w*

RegEx Demo

正则表达式分解:

\b     # word boundary
\w*    # match 0 or more word characters
(\w)   # match a single word char and capture it as group #1
     # back-reference to captured group #1 to make sure we have a *repeat*
\w*    # match 0 or more word characters

顺便说一句,它也会匹配 II 因为它有一个重复字符 I.

匹配所有包含 3 个以上重复字母的单词的模式:

\b\w*(\w){2}\w*

II loooovve 这个视频。这是 awesoooooommeee


https://regex101.com/r/cP7kT7/1