Summary: in this tutorial, we are going to show you how to search and replace strings text using substitution operator s///. We will also introduce you to how to use translation operator tr/// to replace character-by-character in strings.
Substitution
In the previous regular expression tutorials, you have learned how to find the matching text based on a given regular expression. Now you have matching text, but what do you do with it? Normally, you extract it for further processing or replace it with new text. Perl provides substitution operator s/// to allow you to replace the old text, the matching text, with the new text. The following illustrates the substitution operator:
s/regex/newtext/
Code language: Perl (perl)
Between the first two slashes, you put your regular expression. Before the final slash, you put your new text to replace. For example, to replace words new york by New York in a string, you use the expression in the following example:
#!/usr/bin/perl
use warnings;
use strict;
my $str = 'new york city';
$str =~ s/new york/New York/;
print $str, "\n";
Code language: Perl (perl)
The following is the output of the program:
New York city
Press any key to continue . . .
Code language: Perl (perl)
The expression s/new york/New York/ only replaces the first occurrence of new york in the string. In case you want to replace all occurrences of new york in the string, you need to use a regex modifier /g. g stands for global. See the following example:
#!/usr/bin/perl
use warnings;
use strict;
my $str = <<EOF;
new york city is beautiful. Have you ever been to new york city?;
EOF
$str =~ s/new york/New York/g;
print $str, "\n";
Code language: Perl (perl)
The output of the program is:
New York city is beautiful. Have you ever been to New York city?;
Press any key to continue . . .
Code language: Perl (perl)
You can, of course, use the regex modifier /g together with /i (ignore case) to replace all occurrences of a string in-case-sensitively. Let’s take a look at the following example:
#!/usr/bin/perl
use warnings;
use strict;
my $str = <<EOF;
New yorK city is beautiful. Have you ever been to nEw yOrk city?;
EOF
$str =~ s/new york/New York/gi;
print $str, "\n";
Code language: Perl (perl)
The words New yorK and nEw yOrk are replaced with New York because we used modifiers /gi.
One last point to notice that the expression:
$str =~ s/new york/New York/gi;
Code language: Perl (perl)
returns the number of substitutions made. It could be zero (no substitution) or greater than zero. In the previous example, it returns 2. Let’s take a quick test to see how it works:
my $count = ($str =~ s/new york/New York/gi);
print $count;
Code language: Perl (perl)
Translation
In addition to substitution, Perl also provides translation operator tr to allow you to replace character-by-character in strings. For example, if you want to replace every a with b, c with d in the string $str, you use the following expression:
$str =~ tr/ac/bd/;
Code language: Perl (perl)
The expression returns the number of replacements made. The translation operator tr/// is useful in some cases, for example, if you want to count the number of full-stops that appear in a string, you can use the expression in the following program:
#!/usr/bin/perl
use warnings;
use strict;
my $str = "I'm fine. Thank you.";
my $count = ($str =~ tr/././);
print $count, "\n";
Code language: Perl (perl)
In this tutorial, you have learned how to replace the matching text with a new text using substitution operator s/// and replace character-by-character in a string using translation operator tr///.