What is \1 here?
davea at davea.name
Tue Nov 11 13:22:44 CET 2014
satishmlmlml at gmail.com Wrote in message:
> What does \1 do in the following piece of code(fourth line)?
> import re
> print(re.sub('[ABC]', '*', 'XAXAXBXBXCXC'))
> print(re.sub('[ABC]_', '*', 'XA-XA_XB-XB_XC-XC_'))
> print(re.sub('(.) spam', 'spam\\1', 'x spam, y spam'))
> def mapper(matchobj):
> return 'spam' + matchobj.group(1)
> print(re.sub('(.) spam', mapper, 'x spam, y spam'))
Recommend you use raw strings when defining regex patterns. Then
you wouldn't have to double the backslash.
To see what re.sub will see, try
As for what the function does with that, I'll have to guess, as I
don't do regex. I'd guess it'll match any digit.
More information about the Python-list