I am struggling with regEx, but can not get it to work. I already try with: SO question, online tool,
$text = preg_replace("%/\*<##>(?:(?!\*/).)</##>*\*/%s", "new", $text);
But nothing works. My input string is:
$input = "something /*<##>old or something else</##>*/ something other";
and expected result is:
something /*<##>new</##>*/ something other
I see two issues that point out here, you have no capturing groups to replace the delimited markers inside your replacement call and your Negative Lookahead syntax is missing a repetition operator.
$text = preg_replace('%(/\*<##>)(?:(?!\*/).)*(</##>*\*/)%s', '$1new$2', $text);
Although, you can replace the lookahead with .*?
since you are using the s
(dotall) modifier.
$text = preg_replace('%(/\*<##>).*?(</##>*\*/)%s', '$1new$2', $text);
Or consider using a combination of lookarounds to do this without capturing groups.
$text = preg_replace('%/\*<##>\K.*?(?=</##>\*/)%s', 'new', $text);