9951 explained code solutions for 126 technologies


python-regexHow to ignore case in Python regex?


To ignore case in Python regex, use the re.IGNORECASE flag. This flag can be passed as a second argument to re.compile() or re.search() functions.

Example code

import re

pattern = re.compile('hello', re.IGNORECASE)

if pattern.search('Hello World'):
    print('Match found')

Output example

Match found

Code explanation

  • re.IGNORECASE: flag to ignore case in Python regex
  • re.compile(): function to compile a regex pattern
  • re.search(): function to search for a regex pattern

Helpful links

Edit this code on GitHub