[Tutor] Regex/Raw String confusion
Jim Byrnes
jf_byrnes at comcast.net
Thu Aug 4 09:23:46 EDT 2016
On 08/04/2016 03:27 AM, Alan Gauld via Tutor wrote:
> On 04/08/16 02:54, Jim Byrnes wrote:
>
>> Is the second example a special case?
>>
>> phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
>>
>> I ask because it produces the same results with or without the ' r '.
>
> That's because in this specific case there are no conflicts between
> the regex escape codes and the Python escape codes. In other
> words Python does not treat '\(' or '\d' as special characters
> so it doesn't change the string passed to the regex.
> (It would be a different story if you had used, say, a
> '\x' or '\n' or '\b' in the regex.)
>
> In general you should proceed with caution and assume that
> there might be a Python escape sequence lurking in the regex
> and use raw just in case.
>
Ok, thanks again. I understand what is going on now.
Regards, Jim
More information about the Tutor
mailing list