python-regexHow to ignore case in Python regex?
To ignore case in Python regex, use the re.IGNORECASE
flag. This flag can be passed as a second argument to re.compile()
or re.search()
functions.
Example code
import re
pattern = re.compile('hello', re.IGNORECASE)
if pattern.search('Hello World'):
print('Match found')
Output example
Match found
Code explanation
re.IGNORECASE
: flag to ignore case in Python regexre.compile()
: function to compile a regex patternre.search()
: function to search for a regex pattern
Helpful links
More of Python Regex
- How to match whitespace in Python regex?
- How to get a group from a regex in Python?
- How to match HTML tags with regex in Python?
- How to get all matches from a regex in Python?
- How to perform a zero length match with Python Regex?
- How to match a year with Python Regex?
- How to split using Python regex?
- How to replace in a file using Python regex?
- How to replace a certain group using Python regex?
- How to make a case insensitive match with Python regex?
See more codes...