Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for multiline email addresses?

Tags:

regex

I'm looking for a RegEx for multiple line email addresses. For example:

1) Single email:

[email protected]  - ok

2) Two line email:

[email protected]
karensmith@emailcom  - ok

3) Two line email:

john  [email protected] - not ok
karensmith@emailcom

I've tried the following:

((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\r\n)?)+)\r*

But when I test it, it seems to still match ok if there is 1 valid email address as in example 3.

I need a rule which states all email addresses must be valid.

like image 720
jaffa Avatar asked Jun 10 '11 09:06

jaffa


2 Answers

How about:

^(((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\r\n)?\s?)+)*)$

Check the beginning of the string using '^' and the end using '$'. Allow an optional whitespace character with '\s?'.

Try out http://myregexp.com/signedJar.html for testing regex expressions.

like image 126
Mullins Avatar answered Oct 22 '22 05:10

Mullins


I'd split the string on [\r\n]+ and then test each address individualy.

like image 1
Toto Avatar answered Oct 22 '22 04:10

Toto