I think this is the right answer. Pay for the service or software that experts in this problem domain provide for solving the problem. The OP's heuristics will eventually fall short of being adequate, or will generate false positives in some esoteric cases that don't matter until something important gets rejected. There are people for whom email safety is a core competency. For the rest of us, there are better problems to spend time solving.

On the other hand, if a regex solution really is needed, I'd still break it into two; one that triggers if the person's name is detected, and another that validates there is a proper email address, or that flags if there is not.

If it has to be done from a single regex, you could do a negative lookahead, but it just gets more complicated. Here's a working example with a negative lookahead. But I consider it fragile:

#!/usr/bin/env perl use strict; use warnings; use feature qw(say); my @strings = ( 'From: John Doe <peter@piper.com>', 'From: John Doe <john@doe.com>', 'From: John Doe', ); foreach my $string (@strings) { if ($string =~ m/^From:\s+John\s+Doe(?!\s+<john\@doe\.com>)/) { say "BAD: $string" } else { say "GOOD: $string" } }

Dave


In reply to Re^2: Regex Expression to filter email for Barracuda Email Appliance by davido
in thread Regex Expression to filter email for Barracuda Email Appliance by higginss20

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.