Hi all! I'm writing a vulgarity checker for my company that uses a dictionary of regex's to check for bad words. It works great except for one problem I'm having.

This is used for a message board that has a lot of sports related discussions. Some of you may be familiar with the South Carolina Gamecocks, and that's creating a problem in my engine for statements like "I'm a Game Cock fan". The engine basically does this:

study($input); foreach my $word (keys %$vulgar_list) { $regex = $vulgar_list->{$word}->{regex}; if ($input =~ m/$regex/) { $foundvulgar = $word; last; } }

Pardon the profanity here :) What I need is a check for /cocks?/ as long as it isn't preceeded by (a|the|game). Since perl doesn't have a "look behind", and there's no way to do !(a|the|game) within the regex, I'm stumped! I experimented with things like /(?!a|the|game)\s*cock/ but negative lookahead doesn't work that way.

I'm trying to figure out a way to do this in one or two regex's without having a special case in the code for this word (keeping in mind that I have to use a positive regex test with =~).

Any suggestions (even if it has to be multiple regex's) ??

Thanks guys. -Darin

In reply to Regex solution needed by spivey3587

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.