How to write a case insensitive Python regular expression without re.compile?


We can pass re.IGNORECASE to the flags parameter of search, match, or sub −

Example

import re
print (re.search('bush', 'BuSh', re.IGNORECASE))
print (re.match('bush', 'BuSh', re.IGNORECASE))
print (re.sub('bush', 'xxxx', 'Bushmeat', flags=re.IGNORECASE))

Output

<_sre.SRE_Match object at 0x0000000005316648>
<_sre.SRE_Match object at 0x0000000005316648>
xxxxmeat

Updated on: 03-Nov-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements