[Tutor] Regex help

Kent Johnson kent37 at tds.net
Mon Aug 28 17:36:18 CEST 2006

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)

To get the space between 'copy' and 'of' to be included, you have to escape it, e.g. 'copy\ of'.

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.

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''')


More information about the Tutor mailing list