Mega Code Archive

 
Categories / Perl / Regular Expression
 

The g Modifier Global Substitution

#Format: s/search pattern/replacement string/g; # Without the g option while(<DATA>){      print if s/Tom/Jack/;  # First occurrence of Tom on each line is replaced with Jack } __DATA__     Tom Dave Dan Tom     Betty Tom Henry Tom     Jack Norma Tom Tom # With the g option while(<DATA>){      print if s/Tom/Jack/g;  # All occurrences of Tom on each line are replaced with Jack } __DATA__     Tom Dave Dan Tom     Betty Tom Henry Tom     Igor Norma Tom Tom