我是正规表达的新手。
我想从字符串中替换重复的字符。这里有一些例子
$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
Inshort我想替换重复的charactor,后面只有空格。
答案 0 :(得分:4)
您可以使用以下正则表达式:\b(\w)\1+\b
。
说明:
\b
) 编辑:有了更多细节,我会说你可以摆脱第一个\b
。所以,它变成:(\w)\1+\b
答案 1 :(得分: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"
答案 2 :(得分:1)
$text = "aaa bbb cc dddd";
$replacedText = preg_replace('{(\w)\1+}','$1',$text);
如果您不想要重复的空格,请尝试以下方法:
$replacedText = preg_replace('{(.)\1+}','$1',$text);
答案 3 :(得分:1)
尝试类似:
preg_replace('/(\b)(\w)\2+(\b)/', '$2', $string);