Backreferences in lookbehind

Looks like your suspicion is correct that backreferences generally can’t be used in Java lookbehinds. The workaround you proposed makes the finite length of the lookbehind explicit and looks very clever to me. I was intrigued to find out what Python does with this regex. Python only supports fixed-length lookbehind, not finite-length like Java, but … Read more

Can’t use ‘\1’ backreference to capture-group in a function call in re.sub() repr expression

The reason the re.sub(r'([0-9])’,A[int(r’\g<1>’)],S) does not work is that \g<1> (which is an unambiguous representation of the first backreference otherwise written as \1) backreference only works when used in the string replacement pattern. If you pass it to another method, it will “see” just \g<1> literal string, since the re module won’t have any chance … Read more

Negating a backreference in Regular Expressions

Instead of a negated character class, you have to use a negative lookahead: \bvalue\s*=\s*([“‘])(?:(?!\1).)*\1 (?:(?!\1).)* consumes one character at a time, after the lookahead has confirmed that the character is not whatever was matched by the capturing group, ([“”]). A character class, negated or not, can only match one character at a time. As far … Read more