php preg_替换字符串中的分隔符
有一篇很长的文章,我只想删除php preg_替换字符串中的分隔符,php,regex,Php,Regex,有一篇很长的文章,我只想删除千位分隔符,而不是逗号 $str = "Last month's income is 1,022 yuan, not too bad."; //=>Last month's income is 1022 yuan, not too bad. preg_replace('#(\d)\,(\d)#i','???',$str); 如何编写正则表达式模式?谢谢如果您查看了,您可以看到您可以使用$n将捕获写回替换字符串中: preg_replace('#(\d),(\
千位分隔符
,而不是逗号
$str = "Last month's income is 1,022 yuan, not too bad.";
//=>Last month's income is 1022 yuan, not too bad.
preg_replace('#(\d)\,(\d)#i','???',$str);
如何编写正则表达式模式?谢谢如果您查看了,您可以看到您可以使用$n
将捕获写回替换字符串中:
preg_replace('#(\d),(\d)#','$1$2',$str);
请注意,不需要转义逗号,也不需要使用i
(因为模式中没有字母)
另一种方法(可能更有效)是使用。这些不包括在匹配中,因此它们不必写回:
preg_replace('#(?<=\d),(?=\d)#','',$str);
preg#u replace('#)(?如果查看,可以使用$n
将捕获写回替换字符串中:
preg_replace('#(\d),(\d)#','$1$2',$str);
请注意,不需要转义逗号,也不需要使用i
(因为模式中没有字母)
另一种方法(可能更有效)是使用。这些不包括在匹配中,因此它们不必写回:
preg_replace('#(?<=\d),(?=\d)#','',$str);
preg\u replace(”)(?第一个(\d)
由$1
表示,第二个(\d)
由$2
表示。因此,解决方案是使用如下内容:
preg_replace('#(\d)\,(\d)#','$1$2',$str);
实际上,逗号后面最好有3个数字,以避免在数字列表中造成混乱:
preg_replace('#(\d)\,(\d{3})#','$1$2',$str);
第一个(\d)
由$1
表示,第二个(\d)
由$2
表示。因此,解决方案是使用如下内容:
preg_replace('#(\d)\,(\d)#','$1$2',$str);
实际上,逗号后面最好有3个数字,以避免在数字列表中造成混乱:
preg_replace('#(\d)\,(\d{3})#','$1$2',$str);
如果简化规则“匹配直接位于数字之间的任何逗号”对您来说足够好,那么
preg_replace('/(?<=\d),(?=\d)/','',$str);
preg_replace('/(?如果简化规则“匹配直接位于数字之间的任何逗号”对您来说足够好,那么
preg_replace('/(?<=\d),(?=\d)/','',$str);
preg_replace('/)(?请注意,如果用户在列表中犯了愚蠢的错误,您可能会遇到问题:他犯了1、2和3次
(缺少空格-会变成12和3次)。请注意,如果用户在列表中犯了愚蠢的错误,您可能会遇到问题:他犯了1、2和3次
(缺少空格-它将变成12和3次)。