Perl | ‘ee’ Modifier in Regex

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.

s/To_be_replaced/Replacement/

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.

 s///ee;

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.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
my $var = 'for';
  
# Defining the string 
my $String = 'Geeks $var Geeks is the best';
  
# String before using 'ee' modifier
print "Original String: $String\n";
  
# Applying 'ee' modifier using regex
$String =~ s/(\$\w+)/$1/ee;
print "Updated String: $String";

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
my $var = 'for';
  
# Defining the string 
my $String = 'Geeks $var Geeks is the best';
  
# String before using 'e' modifier
print "Original String: $String\n";
  
# Applying 'e' modifier using regex
$String =~ s/(\$\w+)/$1/e;
print "Updated String: $String";

chevron_right


Output:

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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Mathematical expression
# stored as a string
$String = "1 + 2";
  
# Regex to evaluate the sum
$String =~ s/(\d+ [+*\/-] \d+)/$1/ee;
  
print "The sum is $String";

chevron_right


Output:

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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Subroutine to calculate regex
sub Regex
{
    $var = shift;
      
    # Regex to evaluate the sum
    $var =~ s/(\d+ [+*\/-] \d+)/$1/ee;
    return $var;
}
  
# Mathematical expression
# stored as a string
$String1 = "1 + 2";
  
$String1 = Regex($String1);
  
print "The sum is $String1\n";
  
# Calculating product
$String2 = "10 * 3";
$String2 = Regex($String2);
  
print "The product is $String2";

chevron_right


Output:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.