In Python,
re.sub('(ab)c', r'\1d', 'xxxabcxxx')
gives me back 'xxxabdxxx'.
You would expect re.sub('(ab)c', r'\0d', 'xxxabcxxx') to return 'xxxabcdxxx'. That is, you'd expect it to work in a similar way to m.group(0).
However, this isn't supported. http://bugs.python.org/issue17426#msg184210
What is a simple way to achieve what re.sub('(ab)c', r'\0d', 'xxxabcxxx') should achieve, without the use of re.sub()?
Use \g<0>. You can also use \g<1>, etc. for other groups, but 0 is the entire match.
This is explained in the documentation: http://docs.python.org/2/library/re.html#re.sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With