Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Perl | Quantifiers in Regular Expression
Next article icon

Perl | 'e' modifier in Regular Expression

Last Updated : 07 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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 Perl
#!/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"; 
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 Perl
#!/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"; 
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: Perl
#!/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"; 
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.

Next Article
Perl | Quantifiers in Regular Expression

A

Abhinav96
Improve
Article Tags :
  • Perl
  • Perl-regex

Similar Reads

  • Perl | Operators in Regular Expression
    Prerequisite: Perl | Regular Expressions The Regular Expression is a string which is the combination of different characters that provides matching of the text strings. A regular expression can also be referred to as regex or regexp. The basic method for applying a regular expression is to use of bi
    4 min read
  • Perl | Regular Expressions
    Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to the host language and is not the same as in PHP, Python, etc. Sometimes it is termed as “Perl 5 Compatible Regular Expressions“. To use the Rege
    2 min read
  • 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 u
    4 min read
  • Perl - Use of Capturing in Regular Expressions
    A regular expression or a regex is a string of characters that define the pattern that we are viewing. It is a special string describing a search pattern present inside a given text. Perl allows us to group portions of these patterns together into a subpattern and also remembers the string matched b
    3 min read
  • Perl | Quantifiers in Regular Expression
    Perl provides several numbers of regular expression quantifiers which are used to specify how many times a given character can be repeated before matching is done. This is mainly used when the number of characters going to be matched is unknown. There are six types of Perl quantifiers which are give
    4 min read
  • Perl | Backtracking in Regular Expression
    In Perl, a Regular expression(a.k.a regexes or regexps or REs) is a way of describing a set of strings without having to list all strings in your program or simply we can say that it is a sequence of characters that are used for pattern matching. In Perl, regular expressions have different uses: Fir
    3 min read
  • Perl | Special Character Classes in Regular Expressions
    There are many different character classes implemented in Perl and some of them used so frequently that a special sequence is created for them. The aim of creating a special sequence is to make the code more readable and shorter. The Special Character Classes in Perl are as follows: Digit \d[0-9]: T
    4 min read
  • Perl | Grouping and Alternation in Regex
    Regex or Regular Expressions are an important part of Perl Programming. It is used for searching the specified text pattern. In this, set of characters together form the search pattern. It is also known as regexp. Working with regex might become complex with more and more addition in functionalities
    3 min read
  • Perl | Anchors in Regex
    Anchors in Perl Regex do not match any character at all. Instead, they match a particular position as before, after, or between the characters. These are used to check not the string but its positional boundaries. Following are the respective anchors in Perl Regex: '^' '$', '\b', '\A', '\Z', '\z', '
    5 min read
  • Perl | Searching in a File using regex
    Prerequisite: Perl | Regular Expressions Regular Expression (Regex or Regexp or RE) in Perl is a special text string for describing a search pattern within a given text. Regex in Perl is linked to host language and are not the same as in PHP, Python, etc. Sometimes these are termed as "Perl 5 Compat
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences