regular expresions and dolar sign ($) in source string

Jax 77jacek at wp.pl
Thu Apr 23 02:07:17 EDT 2009


Hi

I encountered problem with dolar sign in source string. It seems that $ require
special threatening. Below is copy of session with interactive Python's shell:

Python 2.5.2 (r252:60911, Jan  8 2009, 12:17:37)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> a = unicode(r"(instead of $399.99)", "utf8")
>>> print re.search(unicode(r"^\(instead of.*(\d+[.]\d+)\)$", "utf8"),
a).group(1)
9.99
>>> print re.search(unicode(r"^\(.*(\d+[.]\d+)\)$", "utf8"), a).group(1)
9.99
>>> print re.search(unicode(r"^\(.*\$(\d+[.]\d+)\)$", "utf8"), a).group(1)
399.99

My question is: Why only third regular expression is correct?

Please help! It boggles my mind.

Jax



More information about the Python-list mailing list