1

I have a simple email validation regex to create.

For the moment I have this:

#! /usr/bin/env perl

print "mail from: ";
my $mail_from = lc(<STDIN>);

if ($mail_from =~ /(([^@]+)@(.+))/) {
  $mail_from = $1; 
  print $mail_from;
  print "250 OK\n";
}
else{
  print "550 ERROR \n";
}

But the problem is that I can enter various character after the .com like me@gmail.com blabla

How can I match the string until the first whitespace ?

Regards,

hwnd
  • 69,796
  • 4
  • 95
  • 132
abuteau
  • 6,963
  • 4
  • 16
  • 20
  • 1
    You can get various character *before* the @, too. – choroba Nov 29 '13 at 21:55
  • possible duplicate of [Using a regular expression to validate an email address](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) – Borodin Nov 30 '13 at 10:32
  • [This has been answered here](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?lq=1) Also, see [Email::Valid](http://search.cpan.org/~rjbs/Email-Valid-1.192/lib/Email/Valid.pm) – codnodder Nov 29 '13 at 21:59

4 Answers4

3

Perhaps try a pattern like this:

/(([^@]+)@(\S+))/

\S will match any non-whitespace character.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

The . in your regex matches any and all characters, including whitespace. Try \S instead, which matches any non-whitespace.

Carl Anderson
  • 3,446
  • 1
  • 25
  • 45
0

Use \S to match non-whitespace, and you can slightly simplify the previous term by using a reluctant (a.k.a. non-greedy) quantifier:

/((.*?)@(\S+))/
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

Surely you don't need three captures in your regex?

You probably want to avoid angle brackets before and after the @ as well.

I suggest at least this

/([^<>@\s]+\@[^<>@\s]+)/
Borodin
  • 126,100
  • 9
  • 70
  • 144