如何用正则表达式模式替换正则表达式模式? [英] How to replace a regex pattern with a regex pattern?

查看:126
本文介绍了如何用正则表达式模式替换正则表达式模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对无法解决这一问题感到非常傻,但是我却无法解决。我需要用一个模式替换一个正则表达式字符串,我发现了两个例子,但是老实说,它们使我比以往更加困惑。

I'm feeling very silly for not being able to figure this one out, but I just cant figure it. I need to replace a regex string with a pattern, I found two examples of doing it, but they honestly left me more confused than ever.

这是我目前的尝试:

$string = '26 14:46:54",,"2011-08-26 14:47:02",8,0,"BUSY","DOCUMENTATION","1314370014.18","","","61399130249","7466455647","from-internal","""Oh Snap"" <61399130249>","SIP/1037-00000014","SIP/CL-00000015","Dial","SIP/CL/61436523277,45","2011-08-26 14:47:06","2011-08-26 14:47:15","2011-08-26 ';
$pattern = '["SIP/CL/\d*?,\d*?",]';
$replacement = '"SIP/CL/\1|\2",';
$string = preg_replace($pattern, $replacement, $string);
print($string);

但这只是替换 \1 \2 带空格。因此,显然我并没有理解整个概念。

But that just replaces the \1 and \2 with blanks. So obviously I'm not getting the entire concept.

我最后想要的是更改:

this: "SIP/CL/61436523277,45"
to: "SIP/CL/61436523277|45"

格式不正确的CSV逗号会抛出其他一些脚本。

That comma in a poorly formatted CSV throws off some of my other scripts.

推荐答案

您缺少括号,该模式应如下所示:

You're missing braces, the pattern should look like this:

$pattern = '["SIP/CL/(\d*),(\d*)",]';

这篇关于如何用正则表达式模式替换正则表达式模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆