Problem with regular expression

Pieter Claerhout Pieter_Claerhout at CreoScitex.com
Wed Sep 27 08:08:46 EDT 2000


Looks a bit unlogical to me. The rules say that you either need to escape the backslash or use the raw notation (according to the docs). Why do I need to both escape the backslash here and use the raw notation if I want to match a single backslash??

Pieter

-----Original Message-----
From: Arpad Kiss [mailto:sekter at matavnet.hu]
Sent: Wednesday, September 27, 2000 1:29 PM
To: python-list at python.org
Subject: Re: Problem with regular expression


Hi,
Try this: p=re.compile(r'[A-Za-z]:\\')
Arpad

Pieter Claerhout <Pieter_Claerhout at CreoScitex.com> wrote in message
news:mailman.970053100.5550.python-list at python.org...
> Hello all,
>
> I'm trying to make a regular expression that looks like the following:
>
> p = re.compile('[A-Za-z]:\\')
> if p.match(filepath): return 'pc'
>
> It looks right to me, but whenever I run the program, it fails with the
following traceback:
>
> Traceback (innermost last):
>   File "C:\WINNT\Profiles\PCLAER~1\Desktop\PathTest.py", line 11, in ?
>     print FileUtils.GetPathPlatform(path)
>   File "C:\WINNT\Profiles\PCLAER~1\Desktop\FileUtils.py", line 119, in
GetPathPlatform
>     p = re.compile('[A-Za-z]:\\')
>   File "d:\Python\Lib\re.py", line 79, in compile
>     code=pcre_compile(pattern, flags, groupindex)
> pcre.error: ('\\ at end of pattern', 10)
>
> So it looks like we can't have a backslash at the end of a pattern. Is
this true? If so, how can I work around that, as any type of character can
go after the backslash, except for these characters:
>
> \ / : * ? " < > |
>
> Any ideas? Just worth mentioning: I'm (still) using Python 1.5.2...
>
> Thanks,
>
>
>
> Pieter
>


-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list