0

I'm new to pegexp and I want to write a short one which validates emails. Here is my regexp:

^[a-zA-Z0-9_.-]+@[_a-zA-Z0-9-]+\.[_a-zA-Z0-9-.]+$

The valid email consists of part1@part2. Where part1 consists of 1 or more words, divided by '.' and part2 consists of 2 or more words, divided by '.' Each word consists of a-z or A-Z or 0-9 or - or _

These emails are NOT correct:

smth...smth@smth.smth...

...@a...

I don't know how to fix my regexp and exclude theese examples.

user2178460
  • 5
  • 1
  • 3
  • 1
    It's a little more complicated than that: http://www.regular-expressions.info/email.html. – Boris the Spider Apr 08 '13 at 07:29
  • (?:[A-Za-z0-9-])+@(?:[A-Za-z0-9-]+\.)+(?:[A-Za-z0-9-]) this one fails on smth@smth. (dot at the end is not correct) – user2178460 Apr 08 '13 at 07:38
  • Might wanna give the search box a try. This should cover your needs I'd imagine: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – rvalvik Apr 08 '13 at 12:09

1 Answers1

0

Solved this myself:

/^[a-zA-Z0-9\-_]+(\.([a-zA-Z0-9\-_])+)*@[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+(\.([a-zA-Z0-9\-_])+)*$/
user2178460
  • 5
  • 1
  • 3