regex beginner

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|cd matches ab or cd, but a(b|c)d matches abd or acd.
  • 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|caterpillar matches cat on caterpillar, 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:

  • | separates apple, banana, and cherry as alternatives.
  • qr// is a Perl operator to compile regexes, adding clarity and efficiency.
  • The if statement 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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Matched: apple
Matched: banana
Matched: cherry
No match for: date
No match for: grape
STDERR
(empty)

Was this helpful?

Related Questions