Regular Expressions   «Prev  Next»
Lesson 6 Substitution operator
Objective Examine structural details of using the substitution operator.

Perl Substitution Operator

In the next several lessons, we will cover the rest of the details about the substitution s// operator, which we introduced in the previous module, including how to use it for search-and-replace operations. The s// operator searches a string for the pattern specified with the regex in the left-hand side of the operator (for example, between the first two delimiters). It then replaces the matched pattern with the string on the right-hand side (between the second and third delimiters).
For example,
 
while(<>) { 
 s/the/el/g; 
 print;
}

will replace every occurrence of the with el in the input stream. The s// operator naturally binds to the special $_ variable, unless another variable is provided with one of the binding operators, =~ or !~. So if your intention is to operate on a particular variable, you must specify it with =~ or !~; for instance:
$pathname =~ s|^/|/var/web/|;

That will prepend /var/web/ to any pathnames that start with the root directory.
Note: Notice the use of | for delimiters to avoid a conflict with the slashes in the pathname. This is a very handy trick that you will want to use in an upcoming exercise. The following is an example of when you might use alternative delimiters using the perl-e-modifier.

Describe Structural Details of using the substitution operator in Perl Regex

The substitution operator in Perl, denoted as `s///`, is used to search for a pattern within a string and replace it with a specified replacement string. The basic syntax of the substitution operator is as follows:
s/PATTERN/REPLACEMENT/FLAGS;

Here is a detailed breakdown of the structural components:
  1. PATTERN
    • The PATTERN is a regular expression that defines what you want to search for in the string.
    • It can include literal characters, metacharacters, character classes, quantifiers, and other regex constructs.
    • Example: s/foo/bar/ will search for the substring "foo" and replace it with "bar".
  2. REPLACEMENT
    • The REPLACEMENT is the string that will replace the matched PATTERN.
    • It can include literal text, as well as special variables and escape sequences.
    • Example: s/foo/bar/ will replace "foo" with "bar".
  3. FLAGS
    • Flags are optional modifiers that change the behavior of the substitution operation. They are appended after the last delimiter.
      Common flags include:
      • g (global): Replaces all occurrences of the pattern in the string, not just the first one.
      • i (case-insensitive): Makes the pattern matching case-insensitive.
      • e (evaluate): Evaluates the replacement string as an expression.
      • m (multi-line): Treats the string as multiple lines, allowing ^ and $ to match the start and end of lines within the string.
      • s (single-line): Treats the string as a single line, allowing . to match newline characters.
      • x (extended): Allows whitespace and comments within the pattern for better readability.


Different Types of Substitution

  • Basic Substitution
    my $string = "The quick brown fox jumps over the lazy dog.";
    $string =~ s/fox/cat/;
    print $string;  # Output: "The quick brown cat jumps over the lazy dog."
    
  • Global Substitution
    my $string = "foo foo foo";
    $string =~ s/foo/bar/g;
    print $string;  # Output: "bar bar bar"
    
  • Case-Insensitive Substitution
    my $string = "Foo foo FOO";
    $string =~ s/foo/bar/gi;
    print $string;  # Output: "bar bar bar"
    
  • Using Capture Groups
    my $string = "John Doe";
    $string =~ s/(\w+)\s(\w+)/$2, $1/;
    print $string;  # Output: "Doe, John"
    
  • Evaluating Replacement as an Expression
    my $string = "2 + 2 = 4";
    $string =~ s/(\d+)\s*\+\s*(\d+)/$1 + $2/ge;
    print $string;  # Output: "2 + 2 = 4"
    

Special Variables and Escape Sequences in REPLACEMENT
  • $&: The entire matched string.
  • $`: The string before the match.
  • $': The string after the match.
  • $1, $2, etc.: The text captured by the corresponding capturing group.
  • \U: Converts the following characters to uppercase.
  • \L: Converts the following characters to lowercase.
  • \E: Ends the case conversion started by \U or \L.

Example with Special Variables
my $string = "hello world";
$string =~ s/(\w+)/\U$1/;
print $string;  # Output: "HELLO world"

Modifiers and Context
  • The substitution operator can be used in different contexts, such as within an `if` statement to check if a substitution occurred:
      if ($string =~ s/foo/bar/) {
          print "Substitution occurred.\n";
      }
      

    It can also be used with the `=~` operator to bind it to a specific string.

Summary: The substitution operator `s///` in Perl is a powerful tool for search-and-replace operations using regular expressions. Its flexibility and the ability to use various flags and special variables make it highly versatile for string manipulation tasks.
In the next lesson, we will examine the modifiers that can be used with the s// operator.

SEMrush Software