RegEx: Look-back to avoid an odd number of consecutive backslashes

I have user input where some tags are allowed inside square brackets. I already wrote a regex pattern to find and verify what's inside the brackets.

In the input field of the user input field ([), a backslash can be escaped, and a backslash can be escaped with another backslash (\). I need an external pattern to avoid an odd number of consecutive backslashes before opening.

At the moment, I have to deal with something like this:

(?<!\\)(?:\\\\)*\[(?<inside brackets>.*?)] 

It works fine, but the problem is that this code still matches possible pairs of consecutive backslashes in front of the brackets (even they are hidden), and look-behind just checks to see if there is another backslash attached to the pairs (or directly to the opening - a bracket). I need to avoid them all inside the gaze group, if possible.

Example:

 my [test] string is ok my \[test] string is wrong my \\[test] string is ok my \\\[test] string is wrong my \\\\[test] string is ok my \\\\\[test] string is wrong ... etc 

I am working with PHP PCRE

+8
php regex pcre lookbehind negative-lookbehind
source share
2 answers

Last time I checked, PHP did not support variable-length lookbehinds. That is why you cannot use the trivial solution (?<![^\\](?:\\\\)*\\) .

The simplest workaround would be to simply combine the whole thing, not just a part of the brackets:

 (?<!\\)((?:\\\\)*)\[(?<inside_brackets>.*?)] 

The difference is that now, if you use this regular expression in preg_replace , you must remember the replacement string prefix to $1 to restore the backslash.

+8
source share

You can do this without any delay (alternating (\\\\|[^\\]) eats anything but one backslash):

 ^(\\\\|[^\\])*\[(?<brackets>.*?)\] 
0
source share

All Articles