174
Chapter 7
You can also include ranges of letters or numbers by using a hyphen.
For example,
the character class
[a-zA-Z0-9]
will match all lowercase letters,
uppercase letters, and numbers.
Note that inside the square brackets, the
normal regular expression
symbols are not interpreted as such. This means you do not need to escape
the
.
,
*
,
?
, or
()
characters with a preceding backslash. For example, the
character class
[0-5.]
will match digits
0
to
5
and a period. You do not need
to
write it as
[0-5\.]
.
By placing a caret character (
^
) just after the character class’s opening
bracket, you can make a
negative character class.
A negative character class
will match all the characters that are
not in the character class. For example,
enter the following into the interactive shell:
>>>
consonantRegex = re.compile(r'[^aeiouAEIOU]')
>>>
consonantRegex.findall('RoboCop eats baby food. BABY FOOD.')
['R', 'b', 'C', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
', 'B', 'B', 'Y', ' ', 'F', 'D', '.']
Now, instead
of matching every vowel, we’re matching every character
that isn’t a vowel.
Dostları ilə paylaş: