Lesson 13: Alternation with the Pipe (|)
One of the most flexible tools in the regular expression toolkit is the pipe symbol |
, which represents "alternation." In simple terms, it allows you to match one of several possible patterns, offering a logical "OR" function in your regex patterns. This lesson will take a comprehensive look at the power of the pipe symbol, understanding how to incorporate it effectively into your regex searches.
Basic Usage of the Pipe Symbol
At its core, the |
symbol allows for choosing between different patterns. For example, the regex cat|dog
will match strings containing either cat
or dog
. This ability to specify alternatives greatly expands the flexibility of regex patterns, accommodating varying textual scenarios.
Grouping with Alternation
To further refine your matches, you can use parentheses ()
to group portions of your pattern. This becomes especially useful with alternation. For instance, the pattern gr(a|e)y
would match both gray
and grey
, catering to different spellings in a unified manner.
Complex Patterns and Alternation
The power of the |
symbol isn't restricted to simple strings. It can be combined with other metacharacters and constructs to create intricate patterns. For instance, (\d{3}-|\(\d{3}\) )?\d{3}-\d{4}
can match phone numbers in various formats, such as 555-1234
, (555) 555-1234
, or 555-555-1234
.
Order of Alternatives Matters
It's crucial to remember that regex processes patterns from left to right. Thus, the order of alternatives in an alternation can influence the match. For patterns with overlapping possibilities, ensure that the most specific patterns are listed before the more general ones to achieve the desired matching behavior.
Exercise 13: Navigating Alternation with the Pipe
In a linguistic study, you are analyzing texts to identify variations in the spelling of the word "dog". While "dog" is the standard spelling, there are common misspellings or variations you need to capture.