Gossamer Forum
Home : General : Perl Programming :

finding exact string match

Quote Reply
finding exact string match
i have a censoring routine that contains the following lines:
Code:
@dirty_words = ('fuck','shit','cocksucker','asshole','cunt','pussy','tits','bastard','bitch','piss','darksites','myrealbox','penis','cialis','viagra');
foreach $dirty_word (@dirty_words) {
if (lc($in{$col}) =~ /$dirty_word/) {
....



$in{$col} is the form input

if i enter shitake (mushroom), it rejects the value because it is matching shit. i have tried multiple things and none work. i'm sure it must be something simple but i can't figure it out. please help.
Quote Reply
Re: [delicia] finding exact string match In reply to
ok i changed to following and it seems to be working:
Code:
if (lc($in{$col}) =~ /\b($dirty_word)\b/) {

Quote Reply
Re: [delicia] finding exact string match In reply to
Hi,

Always a good option to test on something like: https://regex101.com/

Code:
if (lc($in{$col}) =~ /\b($dirty_word)\b/) {

That'll do it :) You could do it simpler though:

Code:
if ($in{$col} =~ /\b($dirty_word)\b/i) {

(no need to lc() if you use //i flag to make the regex insensitive)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] finding exact string match In reply to
i changed it -- i like yours better!
Quote Reply
Re: [delicia] finding exact string match In reply to
You could probably refine it even more, without the foreach loop:

Code:
@dirty_words = ('fuck','shit','cocksucker','asshole','cunt','pussy','tits','bastard','bitch','piss','darksites','myrealbox','penis','cialis','viagra');
my $r = join ("|", @dirty_words);
if ($in{$col} =~ /\b($r)\b/i) {
# bad word match
}

:)

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] finding exact string match In reply to
love it! btw, is it necessary to enclose each dirty word in single quotes?
Quote Reply
Re: [delicia] finding exact string match In reply to
If you are using qw// you can get away with it (assuming you don't have spaces in your badwords);

Code:
@dirty_words = qw/fuck shit cocksucker asshole cunt pussy tits bastard bitch piss darksites myrealbox penis cialis viagra/;

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!