[Tutor] Regex help

William O'Higgins Witteman hmm at woolgathering.cx
Mon Aug 28 18:11:59 CEST 2006


On Mon, Aug 28, 2006 at 11:36:18AM -0400, Kent Johnson wrote:
>William O'Higgins Witteman wrote:
>> Thank you for this. The problem is apparently not my syntax, but
>> something else.  Here is a pared-down snippet of what I'm doing:
>>
>> In [1]: import re
>>     
>> In [2]: pat = re.compile('''
>>         ...:copy of
>>         ...:|
>>         ...:admin
>>         ...:''', re.IGNORECASE | re.VERBOSE)
>>
>> In [3]: pat.search('''\\some\unc\path\Copy of somedarnfilename.exn''')
>>
>> In [4]:
>>
>> I don't get my match, and I really think I should.  Can anyone tell me
>> what I'm missing?  Thanks.
>There are several problems here.
>
>First, when re.VERBOSE claims to ignore whitespace, it isn't kidding. 
>Space, tab and newline are all whitespace, so your re is equivalent to
>
>  pat = re.compile('''copyof|admin''', re.IGNORECASE) [redacted]
>
>To get the space between 'copy' and 'of' to be included, you have to escape it, e.g. 'copy\ of'.

D'oh!  I'm an idjit, thanks for your patience.

>But even if you do escape the space, I'm not sure what you expect to match. Colon is not special to regexes (except in non-grouping parentheses (?:...) ), so your regex expects literal colons in the string, which you don't have.

Um, that is the output of the iPython shell, which I thought you used.
I just copied the output into the window.  It indicates an indent.  I
didn't mean to muddy the waters.  Sorry.

>Finally, in your test string you use backslash characters which you mean to be literal backslashes, not character escapes. You should use a raw string for this:
>  pat.search(r'''\\some\unc\path\Copy of somedarnfilename.exn''')

Excellent, thanks for that.
-- 

yours,

William


More information about the Tutor mailing list