我是正则表达式的新手。
我想替换字符串中的重复字符。下面是一些例子
$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 简而言之,我想替换重复的字符,后面只跟空格。
发布于 2013-10-27 04:31:47
您可以使用以下正则表达式:\b(\w)\1+\b。
解释:
至少一次相同的单词分隔符和单个字符重复(至少一次相同的(\b)
编辑:如果有更多的细节,我会说你可以去掉第一个\b。因此,它变成了:(\w)\1+\b
发布于 2013-10-27 15:48:11
下面的正则表达式适用于任何语言中带有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-27 04:41:04
$text = "aaa bbb cc dddd";
$replacedText = preg_replace('{(\w)\1+}','$1',$text);如果您也不想要重复的空格,请尝试以下操作:
$replacedText = preg_replace('{(.)\1+}','$1',$text);https://stackoverflow.com/questions/19611499
复制相似问题