regex question

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Mon Jan 8 03:15:55 EST 2007


On Sun, 07 Jan 2007 23:57:00 -0800, proctor wrote:

> it does work now...however, one more question:  when i type:
> 
> rx_a = re.compile(r'a|b|c')
> it works correctly!
> 
> shouldn't:
> rx_a = re.compile(makeRE(test))
> give the same result since makeRE(test)) returns the string "r'a|b|c'"

Those two strings are NOT the same.

>>> s1 = r'a|b|c'
>>> s2 = "r'a|b|c'"
>>> print s1, len(s1)
a|b|c 5
>>> print s2, len(s2)
r'a|b|c' 8

A string with a leading r *outside* the quotation marks is a raw-string.
The r is not part of the string, but part of the delimiter.

A string with a leading r *inside* the quotation marks is just a string
with a leading r. It has no special meaning.



-- 
Steven D'Aprano 




More information about the Python-list mailing list