Skip to content
Related Articles

Related Articles

Perl | Greedy and non-greedy match

View Discussion
Improve Article
Save Article
  • Last Updated : 29 Dec, 2019

Regular Expressions(Regex/RE) is a sequence of characters that are used for pattern matching. Or we can say that it is a way of describing a set of strings without having to list all strings in your program. We can apply a regular expression by using the pattern binding operators =~ and !~. Regex operator =~ is for testing a regular expression match.
Example: Let us consider /geeks/ as a regular expression. It will match


but not


Whereas !~ determines to which variable the regex is applied, and negates the result of the match; it returns false if the match succeeds, and true if it fails. Consider the above example, the expressions we got as true will be considered as false and false regex as true. It provides a negation of results obtained while using =~.
Pattern Matching: It is a way of finding out a particular sequence of characters or a pattern within a given string. In Perl, we have three regular expression operators. They are:

Match Regular Expression - m//
Substitute Regular Expression - s///
Transliterate Regular Expression - tr///

Example 1: Using match operator

# Perl program to search the substring "or" 
# in the word GeeksForGeeks
# String to find pattern from
$a = "GeeksForGeeks";
# Using m operator to find substring
if($a = ~m/or/)
    print "Found 'or' in the string "

Here in the above example, pattern matching is done for searching a particular substring in a string using the match operator ‘m//‘. It will return true if the pattern is found in the string.

Example 2: Using Substitute operator

# Perl program to replace the substring "For" 
# in the word GeeksForGeeks
# String to perform substitution on
$a = "GeeksForGeeks";
# Using substitution operator to replace
$a = ~s/For/And/; 
print "$a";

Here, the substitution operator ‘s///‘ is used to substitute a pattern in a string with a given text. If the pattern exists then it will be substituted otherwise it will return false.

Example 3: Using transliterator operator

# Perl program to replace all occurrences
# of a pattern in the string
# String to be used
$string = 'GeeksForGeeks';
# Replace 'G' with 'S' using 
# transliterator operator
$string =~ tr/G/S/;
# Printing the final string
print "$string\n";

Here, the string is searched for all the occurrences of a character ‘G’ and is replaced with another character ‘S’ using transliterator operator ‘tr///‘. It will never return false even if no replacement is done.

Greedy Matching And Non-Greedy Matching
The usual rule for matching in REs is sometimes called “left-most longest“: when a pattern can be matched at more than one place within a string, the chosen match will be the one that starts at the earliest possible position within the string, and then extends as far as possible. Normally Perl pattern matching is greedy. By greedy, we mean that the parser tries to match as much as possible. In the string abcbcbcde, for example, the pattern
Greedy and non-greedy matching /(bc)+/ can match in six different ways as shown in the figure:

In the above image, the third of these matched patterns is “left-most longest, ” also known as greedy. In some cases, however, it may be desirable to obtain a “left-most shortest” or minimal match. We can make a greedy matching into a non-greedy match using ‘?‘ at the end of RE i.e ‘*?‘ matches the smallest number of instances of the preceding subexpression that will allow the overall match to succeed. Similarly, ‘+?‘ matches at least one instance, but no more than necessary to allow the overall match to succeed, and ‘??‘ matches either zero or one instances, with a preference for zero.

Example: Greedy pattern matching

# Perl program to show greedy matching
$var = "Geeks For Geeks";
# Matching pattern from k to s
$var =~ /(k.*s)(.*)$/;
# Printing the resultant string
print($1, "\n");

Here we can see that the code will start to match from k to s and it matches as much as possible.

Example: Non-greedy pattern matching

# Perl program to show non-greedy matching
my $str = "Geeks For Geeks";
# Matching pattern from k to s
$str =~ /.*?\s(.*)/; 
# Printing Resultant string

When comparing with the non-greedy operator, it will match least code.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!