Python re.IGNORECASE being dynamic
- by Adam Nelson
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)