Perl | Grouping and Alternation in Regex - GeeksforGeeks

03 Feb, 2022
Regex or Regular Expressions are an important part of Perl Programming. It is used for searching the specified text pattern. In this, set of characters together form the search pattern. It is also known as regexp. Working with regex might become complex with more and more addition in functionalities. To reduce the complexity, Perl provides us with more operations such as Alternation and Grouping. 
The name itself suggests the mechanism it does, first and foremost its like a (&&) operator which appears in ‘C’ programming language. The ‘&&’ operator in C works as until it finds that all the conditional statements are true it won’t return 1 and returns 0 as soon as it finds a false statement. The alternation in Perl works in both ways simultaneously. Alternation can be done using either the metacharacters ‘|’ or ‘[]’. Following examples will make things clear:Example 1:  
perl
#!/usr/bin/perl # Initializing the string and checking it# against few search patternsmy $str = "geeksforgeeks rocks..."; # prints 1 due to no match (=0)print "1\n" if (($str =~ /gek|for/) == 0); # for word searchif($str =~ /geek|for/){    print "I found $&.\n";}else{    print "no match"}
1
I found geek.
 
So, in the above example, it first checks for ‘geek’ and finds a match in the first alternate pattern returns 1 as soon as it finds a match. But it also checks for ‘for’ too and if it is not present in the string it won’t be bothered as it already returned the match status. If ‘for’ is a match, it checks whether ‘for’ is appearing in the least position of the string as ‘g’ in ‘geeks’ appears at position 0 and ‘f’ in ‘for’ appears at position 5, ‘geek’ gets stored in the last match pattern. Check out the following example: 
 $& - Contains the string matched by the last pattern match.
Example 2:  
perl
#!/usr/bin/perl # Initializing the string and checking it# against few search patternsmy $str = "geeksforgeeks rocks...";print "1\n" if ($str =~ /for|eeks|rock|ge/); # | for word searchif($str =~ /for|eeks|rock|ge/){    print "I found $&.\n";} # for single character searchif ($str =~ /[gfrkc]/){    print "$&"}
1
I found ge.
g
 
Grouping is used to search for a pattern bounded with similar words or patterns, in the beginning, end or in the middle and it also returns the least positioned pattern. Grouping is done using metacharacter ‘()’. Following is an example which will make things clear: Example 1: 
perl
#!/usr/bin/perl# Initializing the string and checking it# against few search patternsmy $str = "Blackbackground Brownbackground";print "1\n" if ($str =~ /(Bron|Back)[a-z]+/);if ($str =~ /(Brown|Black)[a-z]+/){    print "I found $&.\n";} else{    print "no match"}
I found Blackbackground.
 
For better understanding check out the following example:  
perl
#!/usr/bin/perluse warnings;use strict; # Initializing the string and checking it# against few searching patterns # array of stringsmy @mail = ('Ab-@gmail.com',            'd.f@yahoo.com',            '0b_f@bing.com',            'jet@hotmail.con',            's/s@otmail.com'); for(@mail){    # search pattern to check valid mail    # service providers followed with '.com'    print "Valid : $_\n"    if($_ =~ /[a-zA-Z0-9-._]+@(gmail|yahoo|bing|hotmail)(.com)$/)}
Valid : Ab-@gmail.com
Valid : d.f@yahoo.com
Valid : 0b_f@bing.com
 
sumitgumber28
Perl-regex
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl Tutorial - Learn Perl With Examples
Perl | Basic Syntax of a Perl Program
Perl | Inheritance in OOPs
Perl | Opening and Reading a File
Perl | Multidimensional Hashes
Perl | Scope of Variables
Perl | ne operator
Perl | Hashes
Perl | Data Types
Perl | defined() Function