What is \1 here?

Ned Batchelder ned at nedbatchelder.com
Tue Nov 11 06:52:15 EST 2014


On 11/11/14 4:18 AM, satishmlmlml at gmail.com wrote:
> 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'))
>

Maybe you can't tell from the reception you're getting here: People are 
growing tired of your questions about basic Python behavior.  They want 
you to find a way to learn for yourself.  You're asking about regular 
expressions.  The Python docs have an extensive page detailing how they 
work: https://docs.python.org/3/library/re.html

You need to learn how to find this stuff out for yourself. Ben Finney 
even gave you a pointer to a helpful site for experimenting with 
regexes: http://pythex.org/

-- 
Ned Batchelder, http://nedbatchelder.com




More information about the Python-list mailing list