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 there
module which contains the regex enginere.compile(r'[A-Z]+', re.IGNORECASE)
: compiles the regex pattern[A-Z]+
with there.IGNORECASE
flagpattern.search('aBc')
: searches the stringaBc
for the compiled regex patternmatch.group()
: prints the matched string
Helpful links
More of Python Regex
- How to perform a zero length match with 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 use word boundaries in Python Regex?
- How to match a year with Python Regex?
- How to match whitespace in Python regex?
- How to validate an IP using Python regex?
- How to match a UUID using Python regex?
- How to match a URL path using Python regex?
- How to match a URL using Python regex?
See more codes...