Perl Basics   «Prev  Next»

Lesson 11 The substitute operator in Regular Expressions
ObjectiveLearn how to use the s/ substitute operator.

Regular Expression Substitute Operator

Perl's s/ substitute operator is used to change the content of a Perl variable.
The syntax of the s/ operator looks like this:
s/expression1/expression2/

The first expression is a matching expression, as in the m/ operator.
The second expression is used to replace what is matched in the first expression.
For example, you can replace all the occurances of "Joe" with "Bob" using an expression like this:
s/Joe/Bob/

Or, if you wanted to change the background color of a <BODY> tag to red, you could use this expression:

while (<>) {
  s/(<body\s*)(bgcolor\s*=\s*[^\s>]*)/$1bgcolor="#ff0000"/i;
  print;
}

/i switch

Notice the /i switch at the end of the substitute operator. That tells Perl to ignore the case of the input stream, treating upper and lowercase letters as equivalent. The part of the matching expression in parenthesis is called a reference. References are referred to in the replacement expression with special numeric variables, $1, $2, and so on. You may also notice that complex expressions can get difficult to read. Using the /x option switch, the above expression could be written like this:


while (<>) {
  s/
   (            # reference 1 
     <body      # match the start-tag "<body"
   ) 
       \s*      # zero or more white spaces
     (          # reference 2 (discarded if present)
       bgcolor  # match "bgcolor"
       \s*      # white space
       =        # match equal sign
       \s*      # more white space
       [^\s>]*  # zero or more non-space non-">" characters
     ) 
   /$1 bgcolor="#ff0000"/ix; 
  print;
}

Write comments to help others know the intention of your regular expression.
Even without the comments, this is a great leap forward in the art of regular expressions.