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 UUID using Python regex?
- How to match a question mark in Python regex?
- How to match whitespace in Python regex?
- How to match a year with Python Regex?
- How to match a plus sign in Python regex?
- How to match a URL path using Python regex?
- How to remove numbers from a string using Python regex?
- How to replace all using Python regex?
- How to ignore case in Python regex?
- How to match an IP address with regex in Python?
See more codes...