I'm playing around with group backreferences in Python's Regex to try to understand them and I'm not having much luck.
import re
leftQuotes = re.compile("((\"|\“)([\w|\d]))")
rightQuotes = re.compile("(([\w|\d])(\"|\”))")
s = "This is “problematic”"
s = re.sub(leftQuotes, r'‘\3', s)
s = re.sub(rightQuotes, r'’\3', s)
print(s)
Output:
This is ‘problemati’”
In the first re.sub(), I managed to successfully replace the left double quotation mark with a single left quotation mark while keeping the matching character (in this case, a "p"). But the right side doesn't behave in the same way, regardless of the group backreference (1, 2, 3).
Results of backreferences:
\1: ‘problemati’c”
\2: ‘problemati’c
\3: ‘problemati’”
s = re.sub(rightQuotes, r'\2’', s), or better just remove unnecessary groups to only keep one that you need to keep, then just use Group 1 backreference.This is ‘problemati’cThis is ‘problemati’c”