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
  • 67,942
  • 4
  • 86
  • 123
abuteau
  • 5,793
  • 4
  • 14
  • 19
  • 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
  • 141,205
  • 29
  • 278
  • 318
1

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

Carl Anderson
  • 3,439
  • 1
  • 24
  • 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
  • 389,931
  • 88
  • 552
  • 692
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
  • 125,056
  • 9
  • 69
  • 143