The Pipe Symbol
The pipe symbol allows you to specify two or more patterns that the regular expression engine uses in
a logical OR formula when examining the data stream. If any of the patterns match the data stream
text, the text passes. If none of the patterns match, the data stream text fails.
This is the syntax for using the pipe symbol:
expr1|expr2|...
Here’s an example of this:
Click here to view code image
>>> re.search('cat|dog', 'The cat is sleeping')
<_sre.SRE_Match object at 0x0154FC28>
>>> re.search('cat|dog', 'The dog is sleeping')
<_sre.SRE_Match object at 0x01570C98>
>>> re.search('cat|dog', 'The horse is sleeping')
>>>
This example looks for the regular expression cat or dog in the data stream.
You can’t place any spaces within the regular expressions and the pipe symbol, or they’ll be added to
the regular expression pattern.
Grouping Expressions
Regular expression patterns can be grouped using parenthesis. When you group a regular expression
pattern, the group is treated like a standard character. You can apply a special character to the group
just as you would to a regular character. Here’s an example:
Click here to view code image
>>> re.search('Sat(urday)?', 'Sat')
<_sre.SRE_Match object at 0x00B07960>
>>> re.search('Sat(urday)?', 'Saturday')
<_sre.SRE_Match object at 0x015567E0>
>>>
The grouping of the day ending along with the question mark allows the pattern to match either the
full day name or the abbreviated name.
It’s common to use grouping along with the pipe symbol to create groups of possible pattern matches,
as shown here:
Click here to view code image
>>> re.search('(c|b)a(b|t)', 'cab')
<_sre.SRE_Match object at 0x015493C8>
>>> re.search('(c|b)a(b|t)', 'cat')
<_sre.SRE_Match object at 0x0157CCC8>
>>> re.search('(c|b)a(b|t)', 'bat')
<_sre.SRE_Match object at 0x015493C8>
>>> re.search('(c|b)a(b|t)', 'tab')
>>>
The pattern (c|b)a(b|t) matches any combination of the letters in the first group along with any
combination of the letters in the second group.