How to I do a search for @anydomain.anyTLD using regular expressions?

link|improve this question
feedback

1 Answer

up vote 3 down vote accepted

@[a-zA-Z0-0\-]*\.[a-zA-Z\.]*

You could improve that but I don't think n++ deals with more complicated regex patterns.

Ideally we'd just use something like

/@[a-z0-9\-]\.[a-z\.\-]{1,40}/i

Which supports everything from the iana TLD list But N++ doesn't support that.

And if we want to get fancy we could do

(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Which supports RFC2822 (see here for more details)

link|improve this answer
1  
.co.uk would like to speak to you, it's 4 characters. I've found that exact regex matching is often far more trouble than it's worth - for example with to. A more exact regex might miss it, but I was using (https?://[^\s]*), a much more general expression, and it worked fine. – Phoshi Jun 8 '10 at 20:21
You are most correct, I have changed the above to work with websites such as .co.uk. – Incognito Jun 8 '10 at 20:30
1  
@user25866: That wasn't my point. How about .org.uk? Indeed, the current technical maximum for a tld is 64 characters, and to is the TLD! +1 for a correct answer, but your "ideal" regex is far from it. – Phoshi Jun 8 '10 at 20:35
Added one that gets real fancy. – Incognito Jun 8 '10 at 20:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.