Python re.IGNORECASE being dynamic

Posted by Adam Nelson on Stack Overflow See other posts from Stack Overflow or by Adam Nelson
Published on 2010-04-23T20:38:56Z Indexed on 2010/04/23 20:43 UTC
Read the original article Hit count: 334

Filed under:
|

I'd like to do something like this:

re.findall(r"(?:(?:\A|\W)" + 'Hello' + r"(?:\Z|\W))", 'hello world',re.I)

And have re.I be dynamic, so I can do case-sensitive or insensitive comparisons on the fly. This works but is undocumented:

re.findall(r"(?:(?:\A|\W)" + 'Hello' + r"(?:\Z|\W))", 'hello world',1)

To set it to sensitive. Is there a Pythonic way to do this? My best thought so far is:

if case_sensitive:
   regex_senstive = 1
else:
   regex_sensitive = re.I
re.findall(r"(?:(?:\A|\W)" + 'Hello' + r"(?:\Z|\W))", 'hello world',regex_sensitive)

© Stack Overflow or respective owner

Related posts about python

Related posts about regex