2

我是正则表达式的新手。

我想从我的字符串中替换重复的字符。这里有一些例子

$str1 = "aaa bbb cc dddd";  // output : a b c d

$str2 = "Google is the best";  // output : Google is the best

我在stackoverflow上发现了很多与这个问题相关的问题。但这并不能满足我的要求。

我试过了(\w)\1,但这不是我的解决方案

任何想法 ?提前致谢

编辑 :

更多示例

 $str1 = "this is tesaaat. are you ook?";  // output : this is tesaaat. are you ook?

 $str2 = "Good morning mmmm yyyy friendssss ";  // output : Good morning m y friends

 $str3 = "Hello friendd okk";  // output : Hello friend okk 

简而言之,我想替换重复的字符,然后只替换空格。

4

4 回答 4

4

您可以使用以下正则表达式:\b(\w)\1+\b.

解释:

  • 一个单词中断 ( \b)
  • 单个字符
  • 重复(至少一次相同的字符)
  • 再次,一个词中断

编辑:有了更多细节,我想说你可以摆脱第一个\b. 所以,它变成:(\w)\1+\b

于 2013-10-26T20:31:47.027 回答
3

u以下正则表达式适用于带有-unicode 标志的任何语言的所有字母:

/([\p{L}\W])\1+(?= )/u

说明:

(                 # beginning of 1st capturing group
    [             # beginning of characters class
        \p{L}     # any letter from any language
        \W        # any non-word character
    ]             # end of character class
)                 # end of 1st capturing group
\1                # back reference to our 1st capturing group for repetition
+                 # one or more character repetition
(?= )             # using positive lookahead to be sure it's followed by a space

用于preg_replace完成工作:

$string = preg_replace("/([\p{L}\W])\1+(?= )/u", "$1", $string);

您的示例的输出:

"aaa bbb cc dddd "  =>  "a b c d "
"Google is the best"  =>  "Google is the best"
"this is tesaaat. are you ook?"  =>  "this is tesaaat. are you ook?"
"Good morning mmmm yyyy friendssss "  =>  "Good morning m y friends "
"Hello friendd okk"  =>  "Hello friend okk"

现场演示

于 2013-10-27T07:48:11.237 回答
1
$text = "aaa bbb cc dddd";
$replacedText = preg_replace('{(\w)\1+}','$1',$text);

如果您也不想要重复的空格,请尝试以下操作:

$replacedText = preg_replace('{(.)\1+}','$1',$text);
于 2013-10-26T20:41:04.210 回答
1

尝试类似:

preg_replace('/(\b)(\w)\2+(\b)/', '$2', $string);
于 2013-10-26T20:46:03.417 回答