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 float with regex in Python?
- How to match a URL using Python regex?
- How to match a plus sign in Python regex?
- How to get all matches from a regex in Python?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match HTML tags with regex in Python?
- How to use word boundaries in Python Regex?
- How to split using Python regex?
- How to use negative lookbehind in Python regex?
- How to ignore case in Python regex?
See more codes...