a newbie regex question

Max Erickson maxerickson at gmail.com
Fri Jan 25 18:57:30 EST 2008


"Dotan Cohen" <dotancohen at gmail.com> wrote:
> Maybe you mean:
> for match in re.finditer(r'\([A-Z].+[a-z])\', contents):
> Note the last backslash was in the wrong place.

The location of the backslash in the orignal reply is correct, it is 
there to escape the closing paren, which is a special character:

>>> import re
>>> s='Abcd\nabc (Ab), (ab)'
>>> re.findall(r'\([A-Z].+[a-z]\)', s)
['(Ab), (ab)']

Putting the backslash at the end of the string like you indicated 
results in a syntax error, as it escapes the closing single quote of 
the raw string literal: 

>>> re.findall(r'\([A-Z].+[a-z])\', s)
	   
SyntaxError: EOL while scanning single-quoted string
>>> 


max





More information about the Python-list mailing list