home ~ projects ~ socials

Do A Case Insensitive RexEx Search In Python

Use the re.IGNORECASE flag to do case insenstive searches. For example, here's the same search with and without it:

import re

alfa  = re.search('test', 'TeSt')
bravo = re.search('test', 'TeSt', re.IGNORECASE)

print(alfa)
print(bravo)
Output:
None
<re.Match object; span=(0, 4), match='TeSt'>

Notes

  • When doing a .sub(), the flags key should be called explicietly like:

    re.sub('test', 'TeSt', 'source value', flags=re.IGNORECASE)

-- end of line --