backreference
JavaScript – string regex backreferences
Like this: str.replace(regex, function(match, $1, $2, offset, original) { return someFunc($2); })
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
How to match a regex with backreference in Go?
Answering my own question here, I solved this using golang-pkg-pcre, it uses libpcre++, perl regexes that do support backreferences. The API is not the same.
preg_replace: add number after backreference
The solution is to wrap the backreference in ${}. $out = preg_replace( ‘/([aeiou])/’, ‘${1}8’, $in); which will output a8bcde8fghi8j See the manual on this special case with backreferences.
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
Regular expression for duplicate words
Try this regular expression: \b(\w+)\s+\1\b Here \b is a word boundary and \1 references the captured match of the first group. Regex101 example here