In Perl, the regular expression allows performing various operations on a given string with the use of suitable operators. These operators can perform operations like modification of string, the substitution of other substrings, etc. Substitution of a substring in the given string is done with the use of ‘s'(substitution) operator, which takes two operands, one is the substring to be replaced and the other being the replacement string.
Modifiers in Perl are used to match a string with a specific pattern with the use of a regular expression. ‘ee’ modifier in Perl is similar to the ‘\e’ modifier. It is used to evaluate the string on the right-hand side and then further evaluate the result. It is basically the double ‘eval’ operator in Perl. ‘\e’ operator is used to evaluate the string in the right-hand side. ‘\ee’ is one step next to it. It applies the ‘\e’ operator on the string on which ‘\e’ is already applied.
Just like ‘e’ modifier, ‘ee’ modifier can also be used with the ‘g'(globally) modifier to make the changes over all possible substrings in the given string.
Original String: Geeks $var Geeks is the best Updated String: Geeks for Geeks is the best
In the above code, $var is printed as it is before the regex is applied on the string because it is considered as a substring of the given string and is not taken as a variable. But after the application of regex, the ‘ee’ modifier evaluates the value of $var and prints it.
If we use ‘e’ modifier in the above code, then the resulting string will be same as the original string:
Original String: Geeks $var Geeks is the best Updated String: Geeks $var Geeks is the best
This is because when using ‘e’ modifier then the regex will consider the RHS to be the evaluated string which is to be used as a replacement, here RHS is $1 which holds $var, but not the value of it. Hence, when we use ‘ee’ modifier then it will evaluate the RHS again which holds the already eval’d value $var.
Using ‘ee’ modifier with mathematical operations:
If there is a mathematical expression stored in a string then while printing the string its value is not evaluated and is printed as it is. This is because it is considered as a string and as an expression to be evaluated.
The sum is 3
In the above code, the expression is written in Regex using the ‘\d+’ operator for writing one or more digits, [+*/-] character class for operator symbol, and then again a ‘\d+’ for digit. The ‘ee’ modifier evaluates the string and returns the sum of the expression which is then printed using the $1 operator.
The above regex can also be stored in a subroutine to evaluate various other expressions without the need of rewriting the regex.
The sum is 3 The product is 30
In the above code, a single regex can be used to perform four mathematical operations using the subroutine.
- Perl | 'e' modifier in Regular Expression
- Perl | Assertions in Regex
- Perl | Anchors in Regex
- Perl | Regex Cheat Sheet
- Perl | Grouping and Alternation in Regex
- Perl | Regex Character Classes
- Perl | Searching in a File using regex
- Perl | Extract IP Address from a String using Regex
- Perl Tutorial - Learn Perl With Examples
- Perl | Basic Syntax of a Perl Program
- Perl vs C/C++
- Perl | eq operator
- Perl | uc() Function
- Perl | chr() Function
- Perl | ge operator
- Perl | next operator
- Perl | hex Function
- Perl | my keyword
- Perl | ne operator
- Perl | exp Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.