Lesson 22f: Regular Expression Part VI s/// tr///

String Substitution

String substitution allows you to replace a pattern or character range
with another one using the s/// and tr/// functions.

The s/// Function

s/// has two parts: the regular expression and the string
to replace it with: s/expression/replacement/.

1
2
3
4
5
$h = "Who's afraid of the big bad wolf?";
$i = "He had a wife.";
 
$h =~ s/w.+f/goat/;  # yields "Who's afraid of the big bad goat?"
$i =~ s/w.+f/goat/;  # yields "He had a goate."

If you extract pattern matches, you can use them in the replacement
part of the substitution:

1
2
3
4
$h = "Who's afraid of the big bad wolf?";
 
$h =~ s/(\w+) (\w+) wolf/$2 $1 wolf/;
# yields "Who's afraid of the bad big wolf?"

Using a Variable in the Substitution Part

Yes you can:

1
2
3
4
$h = "Who's afraid of the big bad wolf?";
$animal = 'hyena';
$h =~ s/(\w+) (\w+) wolf/$2 $1 $animal/;
# yields "Who's afraid of the bad big hyena?"

Translating Character Ranges

The tr/// function allows you to translate one set of
characters into another. Specify the source set in the first part of
the function, and the destination set in the second part:

1
2
$h = "Who's afraid of the big bad wolf?";
$h =~ tr/ao/AO/; # yields "WhO's AfrAid Of the big bAd wOlf?";

tr/// returns the number of characters transformed, which is
sometimes handy for counting the number of a particular character
without actually changing the string.

This example counts N's in a series of DNA sequences:

Code:

1
2
3
4
5
6
 
  while (my $line = ) {
    chomp $line;   # assume one sequence per line
    my $count = $line =~ tr/Nn/Nn/;
    print "Sequence $line contains $count Ns\n";
  }

Output:

(~) 50% count_Ns.pl sequence_list.txt
Sequence 1 contains 0 Ns
Sequence 2 contains 3 Ns
Sequence 3 contains 1 Ns
Sequence 4 contains 0 Ns
...

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *