[Tutor] regular expressions - backslashes

Justin Ezequiel justin.mailinglists at gmail.com
Thu Apr 6 05:23:29 CEST 2006


a colleague demonstrated a problem he had with regular expressions

>>> apppath = os.path.abspath(os.curdir)
>>> apppath
'C:\\napp_and_author_query\\napp_and_author_query\\a b c d'
>>> template = r'<AppPath>\files'
>>> template
'<AppPath>\\files'
>>> re.sub(r'(?i)<apppath>', apppath, template)
'C:\napp_and_author_query\napp_and_author_query\x07 b c d\\files'
>>> print re.sub(r'(?i)<apppath>', apppath, template)
C:
app_and_author_query
app_and_author_query b c d\files
>>>

for this particular use-case, I myself prefer using string formatting
but I still would like to know what's wrong with the above usage of re.sub

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32


More information about the Tutor mailing list