- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Substitution Operator in Perl
The substitution operator s/// in Perl is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is −
s/PATTERN/REPLACEMENT/;
The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of dog with cat using the following regular expression −
Example
#/user/bin/perl $string = "The cat sat on the mat"; $string =~ s/cat/dog/; print "$string\n";
When the above program is executed, it produces the following result −
The dog sat on the mat
Substitution Operator Modifiers
Here is the list of all the modifiers used with substitution operator.
Sr.No | Modifier & Description |
---|---|
1 | i Makes the match case insensitive. |
2 | m Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary. |
3 | o Evaluates the expression only once. |
4 | s Allows use of . to match a newline character. |
5 | x Allows you to use white space in the expression for clarity. |
6 | g Replaces all occurrences of the found expression with the replacement text. |
7 | e Evaluates the replacement as if it were a Perl statement, and uses its return value as the replacement text. |
- Related Articles
- The ? : Operator in Perl
- The Match Operator in Perl
- The Translation Operator in Perl
- Backstick Operator in Perl
- Substitution Method in Data Structure
- What are the substitution techniques in information security?
- Explain C# Substitution in regular expression
- What is S-Box Substitution?
- The $[ Special Variable in Perl
- The Infinite Loop in Perl
- The carp Function in Perl
- The cluck Function in Perl
- The croak Function in Perl
- The confess Function in Perl
- The G Assertion in Perl

Advertisements