python-regexHow to match a range between two characters with Python regex?
To match a range between two characters with Python regex, you can use the []
character class. This character class allows you to specify a range of characters to match. For example, the following code will match any character between a
and z
:
import re
pattern = re.compile('[a-z]')
print(pattern.match('b'))
Output example
<re.Match object; span=(0, 1), match='b'>
The code consists of the following parts:
import re
: This imports there
module, which provides regular expression matching operations.pattern = re.compile('[a-z]')
: This creates a regular expression pattern object that will match any character betweena
andz
.print(pattern.match('b'))
: This prints the result of matching the characterb
against the pattern.
Helpful links
More of Python Regex
- How to match a YYYY-MM-DD date with Python Regex?
- How to get a group from a regex in Python?
- How to use word boundaries in Python Regex?
- How to perform a zero length match with Python Regex?
- How to match a URL path using Python regex?
- How to regex match excluding a character in Python?
- How to match a year with Python Regex?
- How to replace all using Python regex?
- How to match a hex number with regex in Python?
- How to quote in Python regex?
See more codes...