-1

I have the following regex that matches correctly in https://regex101.com but not when using preg_replace_callback in PHP.

(?<!\\)(?:\\\\)*{(.*?)(:(.*?))?}

It works mostly but it doesn't seem to work for multiple levels of escapes. Example text processing (bold matches correctly, italic shows what should be matching and isn't or what is matching and shouldn't):

{intro} The quick brown {animal} jumped over the lazy {otherAnimal}. Empty {} should also match. Escaped should \{not match}. Escaped escape before \\{should match}. 3 escapes should \\\{not match}. 4 escapes \\\\{should match}.

Note that in PHP, the exact code is this with the doubled slashes since it's in a PHP string:

preg_replace_callback('/(?<!\\\\)(?:\\\\\\\\)*{(.*?)(:(.*?))?}/',...
Kudit
  • 4,212
  • 2
  • 26
  • 32

1 Answers1

0

Thanks to Wiktor I figured out the issue. I was using Heredoc syntax when defining my string so all the escapes were being escaped down. Changed the string to use Nowdoc and everything now works as expected. Thank you!

Kudit
  • 4,212
  • 2
  • 26
  • 32