I am having problems with the nested ‘+’/’-‘ lookahead/lookbehind in regex.
Let’s say that I want to change the '*' in a string with '%' and let’s say that '\' escapes the next character. (Turning a regex to sql like command ^^).
So the string
'*test*'should be changed to'%test%','\\*test\\*'->'\\%test\\%', but'\*test\*'and'\\\*test\\\*'should stay the same.
I tried:
(?<!\\)(?=\\\\)*\* but this doesn't work
(?<!\\)((?=\\\\)*\*) ...
(?<!\\(?=\\\\)*)\* ...
(?=(?<!\\)(?=\\\\)*)\* ...
What is the correct regex that will match the ‘*’s in examples given above?
What is the difference between (?<!\\(?=\\\\)*)\* and (?=(?<!\\)(?=\\\\)*)\* or if these are essentially wrong the difference between regex that have such a visual construction?
To find an unescaped character, you would look for a character that is preceded by an even number of (or zero) escape characters. This is relatively straight-forward.
Unfortunately, many regex engines do not support variable-length look-behind, so we have to substitute with look-ahead:
Replace this with the contents of group 1 and a
%sign.Explanation
The look-ahead makes sure only even numbers of backslashes are taken into account. Anyway, there is no way around matching them into a group, since the look-ahead does not advance the position in the string.