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 replace all using Python regex?
- How to match whitespace in Python regex?
- How to match a hex number with regex in Python?
- How to get a group from a regex in Python?
- How to match zero or one occurence in Python regex?
- How to get the href attribute value from a regex in Python?
- How to replace in a file using Python regex?
- How to ignore case in Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match an IP address with regex in Python?
See more codes...