Home: Perl Programming Help: Regular Expressions:
pattern match

martin barlow
New User

Apr 14, 2005, 5:25 PM

Views: 9653
pattern match

The pattern will contain one email address.

Emails should not be matched unless:
They are from a certain domain (say domain.com)
AND not from a two specific emails (say john@domain.com and david@domain.com)

whatever@whatever.com (not match)
whatever@domain.com (match)
john@domain.com (not match)
david@domain.com (not match)

I am sure there is any easy way, but its a bit beyond my regular expression skills! Thanks for any solutions.Cool

Thaumaturge / Moderator

Apr 17, 2005, 9:54 AM

Views: 9643
Re: [martin barlow] pattern match

It's probably easier in this case to split the email address into local part and domain name (by splitting on @) and then doing string comparison.


use strict;
use warnings;

my %match_domain = ('domain.com' => 1);
my %nomatch_name = (john => 1,
david => 1);

while (<DATA>) {
my ($local, $domain) = split /@/;

print "$_ - ";
if ($match_domain{$domain} and not $nomatch_name{$local}) {
print "Match\n";
} else {
print "No Match\n";


Regexes aren't always the best solution to text matching problems :)

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

martin barlow
New User

Apr 20, 2005, 4:46 AM

Views: 9621
Re: [davorg] pattern match


I agree that the simple solution is often the best. Your solution is elegant. Unfortunately in my case i don't have access to proper PERL from the interface.

I need this for a CRM which allows me to specify PCRE's to match email addresses and deal with them accordingly.

I'll mull over this problem a bit longer......


(This post was edited by martin barlow on Apr 20, 2005, 4:49 AM)

martin barlow
New User

May 18, 2005, 10:05 PM

Views: 9298
Re: [martin barlow] pattern match


look good?

(This post was edited by martin barlow on May 18, 2005, 10:06 PM)