Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to refer to the entire matched expression in re.sub without the use of a group?

Tags:

python

regex

Suppose I want to prepend all occurrences of a particular expression with a character such as \.

In sed, it would look like this.

 echo '__^^^%%%__FooBar' | sed 's/[_^%]/\\&/g'

Note that the & character is used to represent the original matched expression.

I have looked through the regex docs and the regex howto, but I do not see an equivalent to the & character that can be used to substitute in the matched expression.

The only workaround I have found is to use the an extra set of () to group the expression and then refernece the group, as follows.

import re


line = "__^^^%%%__FooBar"
print re.sub("([_%^$])", r"\\\1", line)

Is there a clean way to reference the entire matched expression without the extra group creation?

like image 350
merlin2011 Avatar asked Dec 15 '22 19:12

merlin2011


1 Answers

From the docs:

The backreference \g<0> substitutes in the entire substring matched by the RE.

Example:

>>> print re.sub("[_%^$]", r"\\\g<0>", line)
\_\_\^\^\^\%\%\%\_\_FooBar
like image 66
grc Avatar answered Apr 09 '23 06:04

grc