[Tutor] Regex/Raw String confusion
Jim Byrnes
jf_byrnes at comcast.net
Wed Aug 3 15:49:44 EDT 2016
I am reading Automate The Boring Stuff With Python. In the chapter on
Regular Expressions he talks about the python escape character being a
'\' and regex using alot of backslashes. Then he says, <quote> However,
by putting an r before the first quote of the string value, you can
mark the string as a raw sting, which does not escape characters.</quote>
He give this example:
import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print('Phone number found: ' + mo.group())
A couple of pages later he talks about parentheses having special
meaning in regex and what to do if they are in your text.
<qoute>In this case, you need to escape the ( and ) characters with a
backslash. The \( and \) escape characters in the raw string passed to
re.compile() will match actual parenthesis characters.</qoute>
import re
phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('My phone number is: (415) 555-4242.')
print(mo.group(1))
print()
print(mo.group(2))
Both examples work, but one place he says you can't escape raw strings
and the other he says you can. What am I missing here?
Regards, Jim
More information about the Tutor
mailing list