I'm having troubles with re.sub. I understand from other answers that this is due to the fact I'm referencing to a capturing group that I don't have.
My question is: how do I adjust my code to have a valid group?
s = "hello a world today b is sunny c day"
markers = "a b c".split()
pattern = r'\b' + ' (?:\w+ )?(?:\w+ )?'.join(markers) + r'\b'
text = re.sub(pattern, r'<b>\1</b>', s) # this gives error
I want to have this : "hello <b>a world today b is sunny c</b> day"
You cannot use \1
replacement backreference if there are no capturing groups in the pattern. Add the capturing group to the pattern:
pattern = r'\b(' + ' (?:\w+ )?(?:\w+ )?'.join(markers) + r')\b' # or
^ ^
pattern = r'\b({})\b'.format(r' (?:\w+ )?(?:\w+ )?'.join(markers))
Or, just use the \g<0>
to insert the whole match rather than a capturing group value (then, there is no need amending your regex):
text = re.sub(pattern, r'<b>\g<0></b>', s)
See the Python demo.
You don't have any groups in your regex.
(?:...)
is a non capture group, I guess you want
pattern = r'\b(' + ' (?:\w+ )?(?:\w+ )?'.join(markers) + r')\b'
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