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 validate an IP using Python regex?
- How to replace all using Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match whitespace in Python regex?
- How to match a URL using Python regex?
- How to replace in a file using Python regex?
- How to replace using Python regex?
- How to match a plus sign in Python regex?
- How to match zero or one occurence in Python regex?
- How to ignore case in Python regex?
See more codes...