我是正则表达式的新手。
我想替换字符串中的重复字符。 这里有一些例子
$str1 = "aaa bbb cc dddd"; // output : a b c d
$str2 = "Google is the best"; // output : Google is the best
我在堆栈溢出上发现了很多与这个问题相关的问题。但它不能满足我的要求。
我试过这个('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
简而言之,我想替换重复的字符,然后仅替换空格。
您可以使用
以下正则表达式:'b('w)'1+'b
.
解释:
- 分词符 (
'b
) - 单个字符
- 重复(至少一次相同的字符)
- 再次,分词符
编辑:有了更多细节,我会说你可以摆脱第一个'b
。所以,它变成了:('w)'1+'b
以下正则表达式适用于任何带有 -unicode 标志u
语言的所有字母:
/(['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"
现场演示
$text = "aaa bbb cc dddd";
$replacedText = preg_replace('{('w)'1+}','$1',$text);
如果您不希望重复使用空格,请尝试以下操作:
$replacedText = preg_replace('{(.)'1+}','$1',$text);
尝试类似操作:
preg_replace('/('b)('w)'2+('b)/', '$2', $string);