python-regexHow to make a case insensitive match with Python regex?
To make a case insensitive match with Python regex, use the re.IGNORECASE flag. This flag will cause the regex engine to ignore the case of the characters when matching.
Example code
import re
pattern = re.compile(r'[A-Z]+', re.IGNORECASE)
match = pattern.search('aBc')
print(match.group())
Output example
aBc
Code explanation
import re: imports theremodule which contains the regex enginere.compile(r'[A-Z]+', re.IGNORECASE): compiles the regex pattern[A-Z]+with there.IGNORECASEflagpattern.search('aBc'): searches the stringaBcfor the compiled regex patternmatch.group(): prints the matched string
Helpful links
More of Python Regex
- How to match a YYYY-MM-DD date with Python Regex?
- How to replace all using Python regex?
- How to use word boundaries in Python Regex?
- How to match a question mark in Python regex?
- How to match whitespace in Python regex?
- How to replace a certain group using Python regex?
- How to match a URL using Python regex?
- How to use quantifiers in Python regex?
- How to perform a zero length match with Python Regex?
- How to split using Python regex?
See more codes...