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.
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.
‘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
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
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.
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.
- Perl | Operators in Regular Expression
- Perl | pos() function in Regular Expression
- Perl | Quantifiers in Regular Expression
- Perl | Backtracking in Regular Expression
- Perl | 'ee' Modifier in Regex
- Perl | Regular Expressions
- Perl | Special Character Classes in Regular Expressions
- Perl | Basic Syntax of a Perl Program
- Perl vs C/C++
- Perl | gt operator
- Perl | le operator
- Perl | Scalars
- Perl | ge operator
- Perl | CGI Programming
- Perl | hex 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 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.