I need to remove repetitive words in string so that 'the (the)'
will become 'the'
. Why can't I do it as follows?
re.sub('(.+) \(\1\)', '\1', 'the (the)')
Thanks.
You need to doubly escape the back-reference:
re.sub('(.+) \(\\1\)', '\\1', 'the (the)')
--> the
Or use the r
prefix:
When an "r" or "R" prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.
re.sub(r'(.+) \(\1\)', r'\1', 'the (the)')
--> the
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