my $string = 'fred=abcdef&wilbur=123456&fred=hijk'; #### sub obscure { my ($self, $s) = @_; use constant REPLACEMENT => '*'; my @tags = qw/fred wilbur/; for my $tag (@tags) { $s =~ s{$tag=(.*?)\&}{ "$tag=" . (REPLACEMENT x length($1)) . '&' }gex; $s =~ s/$tag=(.*)$/"$tag="/gex; # WRONG } return $s; }