繁体   English   中英

用正则表达式替换包装(父)字符串,并保留内部子内容

[英]Replace the wrapper (parent) string by regular expression and preserve the inner child content

我有多个格式过时的文件,我想用最新格式替换它们。 我的麻烦之一是查找所有包装字符串并全部替换,而我必须保留其内部内容。

我想将表达式ABC($IGNORE$)XBD([$IGNORE$])->T 这是需要更新其格式的字符串。

输入

... ABC( ......
..... (inner content should not be changed) .....
......) ....

输出

... XBD([ ......
..... (inner content should not be changed) .....
......])->T ....

我该如何实现?

您可以使用下面的正则表达式来匹配嵌套的圆括号:

\bABC(\(((?>[^()]+)|(?-2))*\))

替换为XBD$1->T

观看演示

示例PHP代码

<?php
    $re = "#\bABC(\(((?>[^()]+)|(?-2))*\))#"; 
    $str = "ABC( ......\n..... (inner content should not be changed) .....\n......) "; 
    $subst = "XBD$1->T"; 
    echo $result = preg_replace($re, $subst, $str);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM