Alternation and Grouping

Summary: in this tutorial, you will learn how to use alternation and grouping metacharacters in regular expressions.

Alternation

In some cases, you want to build a regular expression that matches different possible words, for example, apple or orange. In order to do so, you need to use alternation metacharacter (|).

To match apple or orange, you can form the regular expression as follows:

/apple|orange/
Code language: Perl (perl)

Perl tries to match the regular expression at the first possible position in the string. For each character position, Perl tries to match the first alternative. If the first alternative does not match, Perl then tries the next alternative and so on. If no alternative matches, the match fails and Perl moves to the next position in the string.

Let’s take a look at the following examples to see how alternation metacharacter works:

"apple and orange" =~ '/apple|orange|mango/'; # matches apple "apple and orange" =~ '/orange|apple|mango/'; # matches orange
Code language: Perl (perl)

Regex grouping metacharacter

By using alternation metacharacter, each alternative is a regular expression. In some cases, you want alternatives are just a part of the regular expression. For example, you want to match housemate, housemaid or houseman you can use the following regular expression:

/housemate|housemaid|houseman/
Code language: Perl (perl)

The issue with this solution is that you have to repeat the word house triple times.

The grouping metacharacter () is designed to solve this problem. By using grouping metacharacter, Perl treats parts of a regular expression as a single unit. You can group parts of a regular expression by enclosing them in parentheses. For example, to match housemate, housemaid or houseman, you can form the regex as follows:

/house(mate|maid|man)/
Code language: Perl (perl)

It matches house followed by either mate, maid or man.

The following example demonstrates grouping metacharacter in regular expression:

#!/usr/bin/perl use warnings; use strict; my @words = ('housemaid', 'housemate', 'household', 'houseman', 'house'); for(@words){ print("$_\n") if(/house(maid|mate|man)/); }
Code language: Perl (perl)

In this tutorial, you have learned how to use alternation and grouping metacharacter in regular expressions.

Was this tutorial helpful ?