Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid group reference when using re.sub()

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"

like image 955
Fed Avatar asked Feb 09 '18 10:02

Fed


2 Answers

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.

like image 166
Wiktor Stribiżew Avatar answered Oct 16 '22 16:10

Wiktor Stribiżew


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'
like image 1
Toto Avatar answered Oct 16 '22 17:10

Toto