I have a tricky regular expression problem that, as it turns out, may not actually be possible. (But if it is possible, I'm sure someone here will know the answer.)
Here's what the problem boils down to...
my @blacklist = ('evil', 'bad', 'wrong');
my $a = "this string contains no blacklisted tokens";
my $b = "this string is evil and wrong";
# The regex should express the blacklist is such a way that
# it will match on any string which DOES NOT contain any of
# the tokens in the blacklist, and it will fail to match on
# any string which DOES contain tokens from the blacklist.
my $regex = '?????';
if (($a =~ m/$regex/) && !($b =~ m/$regex/)) {
# SUCCESS
}
Anyone have any ideas? I've been tinkering with negative lookahaed (which is not my strong suit), but I don't think that's the right approach.
Ideally, what I'd prefer to do is to just search for the blacklist tokens and then negate the result of my match (so that CLEAN strings always return true), but unfortunately, I'm working with a large established codebase, and the regexes are loaded at runtime. I can't modify any of the surrounding code except the regex itself, and I need the expression to match on strings which don't contain any of the tokens in the set. Very tricky.
Thanks for your help.
--Benji
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.