faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

Using Perl-like flags

Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 24, Andrew Kuchling


"""
Packages: text.regular_expressions
"""

"""
>In python regular expressions, can you use flags like i, g, m as you can
>in perl? I use i, he case independent flag often, but I don't see
>anything about it in the docs.

Yes.  When using the re module, you can either put (?i) in the
pattern, or specify the re.I or re.IGNORECASE flag when compiling a
pattern, like this:
"""

import re
pat = re.compile('Name:', re.IGNORECASE)
print pat.match('name:').span()
# (0, 5)
print pat.match('nAME:').span()
# (0, 5)