[Tutor] Regex help
Kent Johnson
kent37 at tds.net
Sat Aug 26 15:45:04 CEST 2006
William O'Higgins Witteman wrote:
> Hello all,
>
> I've been looking for an example of the regex I need, and so far, I
> haven't found anything. Here's what I need:
>
> 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>
Kent
More information about the Tutor
mailing list