我正在努力使用regEx,但无法让它发挥作用。 我已经尝试过了: SO question,online tool,
$text = preg_replace("%/\*<##>(?:(?!\*/).)</##>*\*/%s", "new", $text);
但没有任何作用。 我的输入字符串是:
$input = "something /*<##>old or something else</##>*/ something other";
并且预期结果是:
something /*<##>new</##>*/ something other
答案 0 :(得分:3)
我看到这里指出了两个问题,你没有捕获组来替换替换调用中的分隔标记,而你的否定先行语法缺少repetition operator。
$text = preg_replace('%(/\*<##>)(?:(?!\*/).)*(</##>*\*/)%s', '$1new$2', $text);
虽然您可以使用.*?
替换前瞻,因为您使用的是s
(dotall)修饰符。
$text = preg_replace('%(/\*<##>).*?(</##>*\*/)%s', '$1new$2', $text);
或者考虑使用lookarounds的组合来执行此操作而不捕获组。
$text = preg_replace('%/\*<##>\K.*?(?=</##>\*/)%s', 'new', $text);
答案 1 :(得分:0)
测试:
$input = "something /*<##>old or something else</##>*/ something other";
echo preg_replace('%(/\*<##>)(.*)(</##>\*/)%', '$1new$3', $input);