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


Groups > comp.lang.python > #80515 > unrolled thread

What is \1 here?

Started bysatishmlmlml@gmail.com
First post2014-11-11 01:18 -0800
Last post2014-11-11 17:35 +0000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#80515 — What is \1 here?

Fromsatishmlmlml@gmail.com
Date2014-11-11 01:18 -0800
SubjectWhat is \1 here?
Message-ID<6720bebb-8263-4fb1-bcef-10dcfdc44905@googlegroups.com>
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'))

[toc] | [next] | [standalone]


#80518

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-11-11 10:03 +0000
Message-ID<mailman.15655.1415700238.18130.python-list@python.org>
In reply to#80515
On 11/11/2014 09:18, satishmlmlml@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'))
>

What did your last skivvy die of, overwork?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#80521

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-11-11 06:52 -0500
Message-ID<mailman.15659.1415706751.18130.python-list@python.org>
In reply to#80515
On 11/11/14 4:18 AM, satishmlmlml@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

[toc] | [prev] | [next] | [standalone]


#80523

FromDave Angel <davea@davea.name>
Date2014-11-11 07:22 -0500
Message-ID<mailman.15661.1415708571.18130.python-list@python.org>
In reply to#80515
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

[toc] | [prev] | [next] | [standalone]


#80545

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-11 17:35 +0000
Message-ID<m3thci$vod$3@dont-email.me>
In reply to#80515
On Tue, 11 Nov 2014 01:18:02 -0800, satishmlmlml wrote:

> What does \1 do in the following piece of code(fourth line)?

It tells you to learn about regex.

http://www.pcre.org/pcre.txt

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web