[Tutor] Regex help
William O'Higgins Witteman
hmm at woolgathering.cx
Mon Aug 28 17:04:24 CEST 2006
On Sat, Aug 26, 2006 at 09:45:04AM -0400, Kent Johnson wrote:
>William O'Higgins Witteman wrote:
>> I want a case-insensitive, verbose pattern. I have a long-ish list of
>> match criteria (about a dozen distinct cases), which should be all "or",
>> so I won't need to be clever with precedence.
>
>Vertical bar | is used to separate 'or' cases in a regex. To make it
>case-insensitive and verbose you can compile with the flags re.VERBOSE
>and re.IGNORECASE. Use the search method of the compiled regex to search
>your string. For example,
>
>In [1]: import re
>
>In [2]: rx = re.compile('foo|bar|baz', re.VERBOSE | re.IGNORECASE)
>
>In [3]: rx.search('Foontastic')
>Out[3]: <_sre.SRE_Match object at 0x00C40640>
>
>In [4]: rx.search('raise the BAR')
>Out[4]: <_sre.SRE_Match object at 0x00E901A8>
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.
--
yours,
William
More information about the Tutor
mailing list