Perl | Greedy and non-greedy match
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
Geeksforgeeks
Computergeeks
but not
Ge12eksg22eeks
Geeksg*eks
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
$a = "GeeksForGeeks" ;
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
$a = "GeeksForGeeks" ;
$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
$string = 'GeeksForGeeks' ;
$string =~ tr /G/S/;
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
$var = "Geeks For Geeks" ;
$var =~ /(k. *s )(.*)$/;
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
my $str = "Geeks For Geeks" ;
$str =~ /.*?\s(.*)/;
print ($1);
|
When comparing with the non-greedy operator, it will match least code.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...