1

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
1
  • You don't have a quantifier for the lookahead-masked (.) match-all. Therefore it would only accept one-character strings in your comments. Also just replacing with new will not reinstantiate the /*<##> comment markers. Commented Sep 6, 2014 at 21:26

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Tested:

$input = "something /*<##>old or something else</##>*/ something other";

echo preg_replace('%(/\*<##>)(.*)(</##>\*/)%', '$1new$3', $input);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.