Perl | Greedy and non-greedy match

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
#!usr/bin/perl  # Perl program to search the substring "or" # in the word GeeksForGeeks  # String to find pattern from$a = "GeeksForGeeks";  # Using m operator to find substringif($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
#!/usr/bin/perl  # 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
#!/usr/bin/perl  # 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 stringprint "$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 MatchingThe 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 patternGreedy 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
#!/usr/bin/perl  # Perl program to show greedy matching$var = "Geeks For Geeks";  # Matching pattern from k to s$var =~ /(k.*s)(.*)$/;  # Printing the resultant stringprint($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
#!/usr/bin/perl  # Perl program to show non-greedy matchingmy $str = "Geeks For Geeks";  # Matching pattern from k to s$str =~ /.*?\s(.*)/;   # Printing Resultant stringprint($1);
When comparing with the non-greedy operator, it will match least code.
Perl-regex
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.