Perl Substitution

by Geethalakshmi 2010-09-17 14:08:50

Perl Substitution


The syntax s/match_value_to_replace/replace_value/ replaces the value searched for with the replace_value. By adding the letter g to the end, will find all possible matches. Otherwise, only the first match found is changed.

$_ = "here where there theresa";
s/ere/at/g; # $_ = hat, what, that, thatsa

$_ = "hello, world";
$new = "goodbye";
s/hello/$new/; # $_ = goodbye, world

$_ = "this is a test";
s/(\w+)/<$1>/g # $_ =

The above works, because $1 is set to the with in the first parenthesized pattern match, which is this case is each word because the 'g' flag is used.
850
like
0
dislike
0
mail
flag

You must LOGIN to add comments