The new pattern, [c-h]at, only matches words where the first letter is between the letter c and the
letter h. In this case, the line with only the word at fails to match the pattern.
You can also specify multiple noncontinuous ranges in a single character class:
Click here to view code image
>>> re.search('[a-ch-m]at', 'The cat is sleeping')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('[a-ch-m]at', 'He hit the ball with the bat')
<_sre.SRE_Match object at 0x01570CD0>
>>> re.search('[a-ch-m]at', "I'm getting too fat")
>>>
The character class allows the ranges a through c and h through m to appear before the at text. This
range rejects any letters between d and g.
The Asterisk
Placing an asterisk after a character signifies that the character may appear zero or more times in the
text to match the pattern, as shown in this example:
Click here to view code image
>>> re.search('ie*k', 'ik')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('ie*k', 'iek')
<_sre.SRE_Match object at 0x01570CD0>
>>> re.search('ie*k', 'ieek')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('ie*k', 'ieeek')
<_sre.SRE_Match object at 0x01570CD0>
>>>
This pattern symbol is commonly used for handling words that have a common misspelling or
variations in language spellings. For example, if you need to write a script that may be used by
people speaking either American or British English, you could write this:
Click here to view code image
>>> re.search('colou*r', 'I bought a new color TV')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('colou*r', 'I bought a new colour TV')
<_sre.SRE_Match object at 0x01570C98>
>>>
The u* in the pattern indicates that the letter u may or may not appear in the text to match the pattern.
Another handy feature is combining the dot special character with the asterisk special character. This
combination provides a pattern to match any number of any characters. It’s often used between two
strings that may or may not appear next to each other in the text:
Click here to view code image
>>> re.search('regular.*expression', 'This is a regular pattern expression')
<_sre.SRE_Match object at 0x0154FC28>
>>>
By using this pattern, you can easily search for multiple words that may appear anywhere in the text.