How to use alternation | in Perl regex?
Question
How to use alternation | in Perl regex?
In Perl regular expressions, the alternation operator | is used to match one pattern or another, basically acting like a logical OR within the regex. It allows you to specify multiple alternatives, and the regex engine tries them from left to right until it finds a match.
Understanding Alternation (|) in Perl Regex
The alternation operator | lets you test multiple options within the same regex pattern. For example, the regex cat|dog will match either the string cat or dog.
Key points about alternation:
- Alternation has low precedence, so you often need parentheses to group alternatives. For example,
ab|cdmatchesaborcd, buta(b|c)dmatchesabdoracd. - The regex engine evaluates alternatives from left to right and stops at the first successful match.
- Alternation respects Perl’s regex features like capturing groups, quantifiers, and anchors.
- The operator is available in all versions of Perl with regex support (Perl 5+).
Common Pitfalls
- Missing parentheses: Without grouping,
|might apply in unexpected locations. - Ordering matters: The engine picks the first matching alternative. For example,
cat|caterpillarmatchescatoncaterpillar, so order your alternatives carefully. - Escaping:
|is a metacharacter and should not be escaped unless you want to match the literal pipe character.
Example: Using Alternation in Perl Regex
The following Perl script demonstrates how to use alternation. It tries to match fruit names from a list and prints the successful matches.
#!/usr/bin/perl
use strict;
use warnings;
my @words = ('apple', 'banana', 'cherry', 'date', 'grape');
# regex with alternation - match apples, bananas, or cherries
my $pattern = qr/apple|banana|cherry/;
foreach my $word (@words) {
if ($word =~ /$pattern/) {
print "Matched: $word\n";
} else {
print "No match for: $word\n";
}
}
Output:
Matched: apple
Matched: banana
Matched: cherry
No match for: date
No match for: grape
Explanation
In the example above:
|separatesapple,banana, andcherryas alternatives.qr//is a Perl operator to compile regexes, adding clarity and efficiency.- The
ifstatement checks each word for matching any of the alternatives.
Summary
The alternation operator | in Perl regex is a powerful way to match one pattern or another. Remember to use parentheses for grouping when necessary, and be mindful of the order of alternatives to avoid unexpected matches. With this simple addition, you can write flexible regex patterns that handle multiple possible matches gracefully.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 7ms
Matched: apple
Matched: banana
Matched: cherry
No match for: date
No match for: grape
(empty)Was this helpful?
Related Questions
- How to validate email addresses using Perl regex?
- How to split a string by regex pattern in Perl?
- How to use greedy vs non-greedy quantifiers in Perl regex?
- How to match start and end of string with ^ and $ in Perl?
- How to use the qr// operator to compile regex in Perl?
- How to match Unicode characters in Perl regex?