in reply to More Regexp Confusion
use strict; use warnings; my @strings = ( q{asdfgGOATaewrjgn}, q{ererGOAskjgbrTslkgjnjng}, q{ccG<!-- comment on status of global geopolitical economics -->OAj +gTsvs}, q{aGO<h3>i hate clowns</h3>ATbbb}); my $needle = q{GOAT}; my @parts = split m{}, $needle; my $notNeedle = qq{[^$needle]*}; my $haystackPatt = q{(} . join($notNeedle, @parts) . q{)}; my $rxHaystack = qr{$haystackPatt}; my $stuff1 = q{^^^}; my $stuff2 = q{+++}; foreach my $string ( @strings ) { print qq{$string\n}; $string =~ s{$rxHaystack}{$stuff1$1$stuff2}; print qq{$string\n\n}; }
It produced this output.
asdfgGOATaewrjgn asdfg^^^GOAT+++aewrjgn ererGOAskjgbrTslkgjnjng erer^^^GOAskjgbrT+++slkgjnjng ccG<!-- comment on status of global geopolitical economics -->OAjgTsvs cc^^^G<!-- comment on status of global geopolitical economics -->OAjgT ++++svs aGO<h3>i hate clowns</h3>ATbbb a^^^GO<h3>i hate clowns</h3>AT+++bbb
I hope this is useful.
Cheers,
JohnGG
Update: Fixed typos
Update 2: Changed initialisation of $notNeedle so string was not hard coded, original line was my $notNeedle = q{[^GOAT]*};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: More Regexp Confusion
by mdunnbass (Monk) on Feb 05, 2007 at 14:59 UTC |