[英]How to replace a regex pattern with a regex pattern?
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.
我需要用模式替换正则表达式字符串,我发现了两个这样做的例子,但他们老实说让我比以前更加困惑。
This is my current attempt: 这是我目前的尝试:
$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);
But that just replaces the \\1
and \\2
with blanks. 但这只是用空格替换
\\1
和\\2
。 So obviously I'm not getting the entire concept. 显然,我没有得到整个概念。
What I'm wanting at the end is to change: 我最不想要的是改变:
this: "SIP/CL/61436523277,45"
to: "SIP/CL/61436523277|45"
That comma in a poorly formatted CSV throws off some of my other scripts. 格式不正确的CSV中的逗号会抛出我的其他一些脚本。
你缺少大括号,模式应如下所示:
$pattern = '["SIP/CL/(\d*),(\d*)",]';
I think you need this: 我想你需要这个:
$pattern = '["SIP/CL/(\d+),(\d+)",]';
The parens - ()
- capture the match inside, allowing you to reference it in your replacement pattern. parens -
()
- 捕获内部的匹配,允许您以替换模式引用它。
You could also simplify the replacement pattern by expanding the captures in the match pattern: 您还可以通过扩展匹配模式中的捕获来简化替换模式:
$pattern = '[("SIP/CL/\d+),(\d+",)]';
$replacement = '\1|\2';
The backreferences \\1
and \\2
in your $replacement
are meant to refer to captured groups in $pattern
. $replacement
中的反向引用\\1
和\\2
是指$pattern
捕获的组 。 Captured groups are saved by placing brackets around them ()
. 通过在它们周围放置括号
()
来保存捕获的组。
Try (I changed your regex delimiters []
to !
pending an answer from comments below): 尝试(我将你的正则表达式分隔符
[]
更改为!
等待下面评论的回答):
$pattern = '!"SIP/CL/(\d*?),(\d*?)",!';
$replacement = '"SIP/CL/\1|\2",';
Also note that since you have a trailing comma on your $pattern
, if your $string
ended in "SIP/CL/61436523277,45"
that would not be converted. 另请注意,由于
$pattern
上有逗号,因此$string
以"SIP/CL/61436523277,45"
,不会被转换。 I'd recommend removing that trailing comma. 我建议删除那个尾随的逗号。
Also your current regex will convert "SIP/CL/,"
to "SIP/CL/|"
您当前的正则表达式也会将
"SIP/CL/,"
转换为"SIP/CL/|"
. 。 If that is not your intent, change the
*
after the \\d
(ie 0 or more matches) to a +
(one or more matches). 如果这不是您的意图,请将
\\d
(即0或更多匹配)后的*
更改为+
(一个或多个匹配项)。
I did this 我这样做了
$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\/([0-9]+),([0-9]+)/';
$replacement = 'SIP/CL/\1|\2';
$string = preg_replace($pattern, $replacement, $string);
print($string);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.