Perl | Grouping and Alternation in Regex

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.

Alternation

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initializing the string and checking it
# against few search patterns
my $str = "geeksforgeeks rocks...";
  
# prints 1 due to no match (=0)
print "1\n" if (($str =~ /gek|foor/) == 0); 
  
# for word search
if($str =~ /geek|for/) 
{
    print "I found $&.\n";
}
else
{
    print "no match"
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initializing the string and checking it
# against few search patterns
my $str = "geeksforgeeks rocks...";
print "1\n" if ($str =~ /for|eeks|rock|ge/);
  
# | for word search
if($str =~ /for|eeks|rock|ge/)
{
    print "I found $&.\n";
}
  
# for single character search
if ($str =~ /[gfrkc]/)
{
    print "$&"
}

chevron_right


Output:

1
I found ge.
g

Grouping

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
# Initializing the string and checking it
# against few search patterns
my $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"
}

chevron_right


Output:

I found Blackbackground.

For better understanding check out the following example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
use warnings;
use strict;
  
# Initializing the string and checking it
# against few searching patterns
  
# array of strings
my @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)$/)
}

chevron_right


Output:

Valid : Ab-@gmail.com
Valid : d.f@yahoo.com
Valid : 0b_f@bing.com


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.