chunlou has asked for the wisdom of the Perl Monks concerning the following question:
The profanity you match could be a regex pattern, but the word you replace it with can only be some predefined static string. So, if I want to replace "piss me off" or "piss us off" with "lift me up" or "lift us up", I would need to create two key/value pairs in RegexpHash, instead of something like /piss (.*) off/ and /lift $1 up/. Any short neat way to accomplish that?use Tie::RegexpHash; my $text = <<TEXT; God damn you for your birth, you mummaphvker. I'm so PI\$\$\$\$ed, I hope you go to Hell. You have ruined my life. \$KRU you. I H\@TE you forever, you goddamn fvker TEXT print "Before:\n$text\n"; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my %profane; tie %profane, 'Tie::RegexpHash'; # profanity replacements $profane{qr/ruin/i} = 'lighten'; # ruin $profane{qr/hell/i} = 'Heaven'; # hell $profane{qr/(damn|darn)/i} = 'bless'; # damn $profane{qr/h(a|@)te/i} = 'love'; # hate $profane{qr/(f|ph)(u|v)(c*)(k*)er/i} = 'lover'; # f..ker $profane{qr/pi(s+|\$+)/i} = 'touch'; # piss $profane{qr/(s|\$)(c|k)r(ew|oo|u)/i} = 'Kiss'; # screw # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my $profanity = join("|", keys(%profane)); $text =~ s/($profanity)/$profane{$1}/g; print "After:\n$text\n"; __END__ Before: God damn you for your birth, you mummaphvker. I'm so PI$$$$ed, I hope you go to Hell. You have ruined my life. $KRU you. I H@TE you forever, you goddamn fvker After: God bless you for your birth, you mummalover. I'm so touched, I hope you go to Heaven. You have lightened my life. Kiss you. I love you forever, you godbless lover
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Turn Hate Mail to Love Letter: Regex Multi-Word/Phase Replace
by Skeeve (Parson) on Jun 25, 2003 at 08:18 UTC | |
by chunlou (Curate) on Jun 25, 2003 at 12:21 UTC | |
by Skeeve (Parson) on Jun 26, 2003 at 06:22 UTC | |
by chunlou (Curate) on Jun 26, 2003 at 06:40 UTC | |
by Skeeve (Parson) on Jun 26, 2003 at 07:40 UTC | |
|