my %ch = ('green' => 'lousy', 'blue' => 'cool', 'pink' => 'mini' ) ; my $str = 'I have a green hat, blue shirt, plus a pink jacket'; print $str . "\n" ; $str =~ s/(green|blue|pink)/$ch{$1}/g ; print $str ; __END__ I have a green hat, blue shirt, plus a pink jacket I have a lousy hat, cool shirt, plus a mini jacket
use Tie::RegexpHash; my %sr; tie %sr, 'Tie::RegexpHash'; $sr{qr/\bh+(a|@)+t+e+\b/i} = 'love'; $sr{qr/\b(u|you|eww)\b/i} = 'you'; # - - - - - - - - - - - - - - - - -- - - - -- - - - - $_ = "I hate you i HAte u i HH\@\@\@TTeE eww i HA\@AaaTTee u I HATE YO +U!\n"; print; my $s = join("|", keys%sr); s/($s)/$sr{$1}/g; print; __END__ I hate you i HAte u i HH@@@TTeE eww i HA@AaaTTee u I HATE YOU! I love you i love you i love you i love you I love you!
use Tie::RegexpHash; my %sr; tie %sr, 'Tie::RegexpHash'; # - - - - - - - - - - - - - - - - - - - - - - - - - - # search and replace $sr{qr/\b(h)arbour\b/i} = '$2arbor'; $sr{qr/\b(h)onour(.*?)\b/i} = '$3onor$4'; $sr{qr/\b(c)entre\b/i} = '$5enter'; # - - - - - - - - - - - - - - - - -- - - - -- - - - - $_ = "Programmers Honoured at Harbour Centre\n"; print; my $s = join("|", keys%sr); s/($s)/eval'"'.$sr{$1}.'"'/ge; print; __END__ Programmers Honoured at Harbour Centre Programmers Honored at Harbor Center
use Tie::RegexpHash; my %sr; tie %sr, 'Tie::RegexpHash'; # - - - - - - - - - - - - - - - - - - - - - - - - - - # search and replace $sr{qr/\b(c)entre\b/i} = '$5enter'; $sr{qr/\b(h)arbour\b/i} = '$2arbor'; $sr{qr/\b(h)onour(.*?)\b/i} = '$3onor$4'; $sr{qr/(T|t)heatre/} = 'theater'; # - - - - - - - - - - - - - - - - -- - - - -- - - - - $_ = "Programmers Honoured at Harbour Centre\n"; print; my $s = join("|", keys%sr); s/($s)/eval'"'.$sr{$1}.'"'/ge; print; __END__ Programmers Honoured at Harbour Centre Programmers onorH at arbor enter
use Regexp::Subst::Parallel; my @sr =( qr/\b(h)arbour\b/i => '$1arbor', qr/\b(h)onour(.*?)\b/i => '$1onor$2', qr/\b(c)entre\b/i => '$1enter', qr/\b(L|l)ift\b/ => sub{$_=$_[1]=~/L/?"E":"e";$_."leva +tor"} ); # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $_ = "Man Honoured at Harbour Centre by the Lift\n"; print; $_ = subst($_, @sr); print; __END__ Man Honoured at Harbour Centre by the Lift Man Honored at Harbor Center by the Elevator
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: 3 Examples of Multiple-Word Search n Replace
by enoch (Chaplain) on Jun 27, 2003 at 21:20 UTC | |
Re: 3 Examples of Multiple-Word Search n Replace
by japhy (Canon) on Jun 28, 2003 at 04:17 UTC | |
Re: 3 Examples of Multiple-Word Search n Replace
by demerphq (Chancellor) on Jun 28, 2003 at 09:41 UTC | |
by chunlou (Curate) on Jun 28, 2003 at 19:40 UTC | |
by demerphq (Chancellor) on Jun 28, 2003 at 21:38 UTC | |
by chunlou (Curate) on Jun 28, 2003 at 20:50 UTC |