in reply to Defenetly a complicated perl recipie with 2 arrays, 1 scaler and some special matching

You came into #perl on EFNET and didn't wait too long for a response. Here's something that may get you started. I'm sure it can be fine-tuned, as this is something I whipped up in about 5-10 minutes.
my @badwords = ( '31337', 'hacker' ); my @exceptions = ( 'perl_31337_hacker', ); my $string = 'I am a 31337 hacker. A perl_31337_hacker, that is, not a + c_31337_hacker.'; my @occurences = (); my ($word, $oldword, $badword); foreach $word (@badwords) { push(@occurences, $string =~ /\b\S*$word\S*\b/g); } my %exceptions; @exceptions{@exceptions} = @exceptions; foreach $word (@occurences) { if ( !exists ($exceptions{$word}) ) { $oldword = $word; foreach $badword ( @badwords ) { $word =~ s/$badword/<censored>/g; } $string =~ s/$oldword/$word/; } } print $string;
Output:
I am a <censored> <censored>. A perl_31337_hacker, that is, not a c_<c +ensored>_<censored>.
e-Motion
  • Comment on Re: Defenetly a complicated perl recipie with 2 arrays, 1 scaler and some special matching
  • Select or Download Code