Home  >  Article  >  Backend Development  >  正则表达式匹配,并替换部分字符,该如何解决

正则表达式匹配,并替换部分字符,该如何解决

WBOY
WBOYOriginal
2016-06-13 10:13:171159browse

正则表达式匹配,并替换部分字符
$aaa = "aaa;bbb;ccc;ddd;eee;fff;"

替换成:"aaa*bbb;ccc*ddd;eee*fff;"

不要用php的方法!

我只想用正则替换不知哪位同学能帮忙解决下,实在想不出来!
目的就是把奇数位的;替换成* 偶数位的;不变!

先谢谢了~

------解决方案--------------------
echo preg_replace('/(\w+);(\w+)/','$1*$2',$aaa);
------解决方案--------------------
隔一次替换一次是吧?

PHP code
$aaa = "aaa;bbb;ccc;ddd;eee;fff;";echo preg_replace('#\G[^;]+\K;([^;]+;?)#','*\1',$aaa);<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn