Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80523

Re:What is \1 here?

From Dave Angel <davea@davea.name>
Subject Re:What is \1 here?
Date 2014-11-11 07:22 -0500
Organization news.gmane.org
References <6720bebb-8263-4fb1-bcef-10dcfdc44905@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15661.1415708571.18130.python-list@python.org> (permalink)

Show all headers | View raw


satishmlmlml@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
print ('spam\\1')

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.

-- 
DaveA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

What is \1 here? satishmlmlml@gmail.com - 2014-11-11 01:18 -0800
  Re: What is \1 here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-11 10:03 +0000
  Re: What is \1 here? Ned Batchelder <ned@nedbatchelder.com> - 2014-11-11 06:52 -0500
  Re:What is \1 here? Dave Angel <davea@davea.name> - 2014-11-11 07:22 -0500
  Re: What is \1 here? Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-11 17:35 +0000

csiph-web