Perl | ‘e’ modifier in Regular Expression

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, 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/

Further, if there is a need to substitute the substring with a replacement string which is a regular expression to be evaluated, ‘e‘ modifier is used. The ‘e’ modifier is placed at the end of the substitution expression.

s/To_be_replaced/Regular_Expression/e;

‘e’ modifier can also be used with the ‘g'(globally) modifier to make the changes over all possible substrings in the given string.



Example 1: Using character class for substitution

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining the string to be converted
$String = "Geeks for Geeks is the best";
print "Original String: $String\n";
  
# Converting the string to UPPERCASE
# using 'uc' Function
$String =~ s/(\w+)/uc($1)/ge;
print"Uppercased String: $String\n";
  
# Converting the string to lowercase
# using 'lc' Function
$String =~ s/(\w+)/lc($1)/ge;
print"Lowercased String: $String\n";

chevron_right


Output:

Original String: Geeks for Geeks is the best
Uppercased String: GEEKS FOR GEEKS IS THE BEST
Lowercased String: geeks for geeks is the best

Above code uses a character class ‘\w’ which holds the alphabet of lower case and upper case along with all the digits(a-z|A_Z|0-9). This is used to perform a single substitution operation on the whole string.

Example 2: Using single character or a word for specific substitution

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Defining the string to be converted
$String = "Geeks for Geeks is the best";
print "Original String: $String\n";
  
# Converting a single character using e modifier
$String =~ s/(e)/uc($1)/ge;
print"Updated String: $String\n";
  
# Converting a word using e modifier
$String =~ s/(for)/uc($1)/ge;
print"Updated String: $String\n";

chevron_right


Output:

Original String: Geeks for Geeks is the best
Updated String: GEEks for GEEks is thE bEst
Updated String: GEEks FOR GEEks is thE bEst

In the above code, it can be seen that the string after updation will not revert to its original version even after applying the second recursion on it.

Use of a subroutine for the substitution operation:
Substitution operation in Perl regex can also be done with the use of subroutines to avoid the redundancy of writing the substitution regex again and again for every string. This can be done by placing the regex code in the subroutine and calling it wherever required.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Subroutine for substitution operation
sub subroutine
{
    $regex = shift;
    $regex =~ s/Friday/Tuesday/;
    return $regex;
}
  
# Defining the string to be converted
$String = "Monday Friday Wednesday";
print "Original String: $String\n";
  
# Calling the subroutine for substitution
$String =~ s/(\w+)/subroutine($1)/ge;
print"Updated String: $String\n";
  
  
# Defining a new String to be converted
$String2 = "Today is Friday";
print "\nOriginal String: $String2\n";
  
# Calling the subroutine for substitution
$String2 =~ s/(\w+)/subroutine($1)/ge;
print"Updated String: $String2\n";

chevron_right


Output:

Original String: Monday Friday Wednesday
Updated String: Monday Tuesday Wednesday

Original String: Today is Friday
Updated String: Today is Tuesday

In the above code, when the substitution operation begins then it calls the subroutine ‘change_substitution’ which holds the regex code for replacing the substring which matches the search.



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.