Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用正则表达式替换重复字符_Php_Regex - Fatal编程技术网

Php 用正则表达式替换重复字符

Php 用正则表达式替换重复字符,php,regex,Php,Regex,我是正则表达式的新手 我想替换字符串中的重复字符。这里有一些例子 $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 t

我是正则表达式的新手

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

$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 

总之,我只想用空格替换重复的字符。

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

说明:

  • 断字(
    \b
  • 单个字符
  • 重复(至少一次相同字符)
  • 再一次,一个单词中断了
编辑:如果有更多细节,我想说你可以去掉第一个
\b
。因此,它变成:
(\w)\1+\b

$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);

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

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

使用
preg\u 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"

请解释为什么Google中的oo不是重复字符。@我只想替换重复的字符后跟空格。以及'Good'@rawb中的'oo',我在字符串后面显示了输出。以下内容很难看,但适用于Google:
$replacedText=preg_replace(数组('/()\1+/','/Gogle/')、数组('$1','Google'))美元文本)这没问题。但请再提一次问题。我添加了更清晰的示例。答案经过编辑,但仍然不理解为什么这是TESAAAT。
=>
这是tesat。
。是的,你是对的。输出应该是“这是一个tesaaat”。总之,我只想用空格替换重复的字符。不能+1这是一个很好的解释。但是测试这个字符串“Google是最好的”或者“Hello friend”。这对我不起作用。请再提一次问题。我只想用空格替换重复的字符。注意:正则表达式已不再存在。演示效果很好,但正如我在演示中注意到的,与answer的正则表达式不同,这里有修饰符-
u
g
。同样在演示中,正则表达式是
([\p{L}\s])\1+(?=)
,但在答案中是
([\p{L}\W])\1+(?=)
。当我尝试将演示的版本与midifier
g
一起使用时,我会得到一个错误
未知修饰符“g”
,如果我删除它,正则表达式将无法工作。thanks@dav
preg\u replace
在不使用
g
修饰符的情况下将按预期工作,但要进行匹配,应使用
preg\u match\u all
而不是
preg\u match
"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"