Occasionally, you can't use just a /g to get all the changes to occur. Here are two common cases: # put commas in the right places in an integer 1 while s/(.*\d)(\d\d\d)/$1,$2/g; # perl4 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; # perl5 # expand tabs to 8-column spacing 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e; #### # patterns to search with are stored in @pattern Array # patterns are strings of uc DNA (i.e. TTTAGT, etc) # but can be interrupted by lc or non-DNA characters (i.e., html tags) # searching for a needle in a haystack: my $notNeedle = q{[^ACGTYRMKSWBDHVN]*}; while ($g < $num) { $needle[$g] = $pattern[$g]; @{$parts[$g]} = split m/(\[|\]|\|)/, $needle[$g]; my @array = @{$parts[$g]}; my @results; while (@array) { my $string = shift @array; if ($string eq '[') { $string .= (join "", splice(@array, 0, 2)); } if ($string eq '|') { my $prev = pop @results; $string = (join "", $prev, $string, splice(@array, 0, 1)); } push @results, $string; } @{$parts[$g]} = @results; $haystackPatt[$g] = q{(} . join($notNeedle, @{$parts[$g]}) . q{)}; $rxHaystack[$g] = qr{$haystackPatt[$g]}; $g++; } $text_seqs[$i] =~ s{$rxHaystack[$j]}{$1}gemx;