Summary: in this tutorial, you will learn how to use alternation and grouping metacharacters in regular expressions.
In some cases, you want to build a regular expression that matches different possible words, for example,
orange. In order to do so, you need to use alternation metacharacter (|).
orange, you can form the regular expression as follows:
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
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
houseman you can use the following regular expression:
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
houseman, you can form the regex as follows:
It matches house followed by either
The following example demonstrates grouping metacharacter in regular expression:
my @words = ('housemaid',
In this tutorial, you have learned how to use alternation and grouping metacharacter in regular expressions.