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
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
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
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
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
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
When comparing with the non-greedy operator, it will match least code.
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- HTTP headers | If-None-Match
- HTTP headers | If-Match
- Perl vs C/C++
- Perl | GET vs POST in CGI
- Recursion in Perl
- Perl | qq operator
- Formats in Perl
- Perl | Operators | Set - 2
- Perl | qw Operator
- Perl vs Java
- What is the difference between Perl and PHP ?
- Packages in Perl
- Perl | q operator
- Perl | ord() Function
- Perl | my keyword
- Perl | Arrays
- Perl | Operators | Set - 1
- Use of print() and say() in Perl
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.