Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match an email address if it contains a dot

Tags:

c#

regex

I want to match any email address, that contains at least one . (dot) before the @ sign. The emails have already been validated, so the regex just needs to search for the ..

I have tried

Regex emailMatcher = new Regex(@"^[a-zA-Z\.']{1,}\.[a-zA-Z\.']{1,}@example\.com$");

But I know that emails can contain more characters than just a-zA-Z\.' so this won't cover all cases.

Any ideas on how to do it?

Thanks

EDIT: I'm not trying to validate emails, I already have the emails validated, I just need to select emails, that contain . before @ sign

Examples that would pass:

[email protected]
[email protected]

Examples that should pass, but wouldn't pass using my current regex

first.last(comment)@example.com
like image 738
leopik Avatar asked May 06 '15 12:05

leopik


2 Answers

You could do this without a regex

Func<string, bool> dotBeforeAt = delegate(string email) 
{ 
    var dotIndex = email.IndexOf(".");
    return dotIndex > -1 && (dotIndex < email.IndexOf("@"));
};
...
emails.Where(dotBeforeAt).ToList();

Try it out

like image 99
James Avatar answered Sep 26 '22 00:09

James


I just need to select ones, that contain dot before @ sign

Then there is no point to build a regex that matches valid e-mail addresses. All you need is a regex that sees that there is a dot in front of the @ sign:

(?<=[.][^@]*)@

(?<=[.][^@]*) is a positive lookbehind construct. It ensures that the @ sign following it is matched only when there is a dot [.] followed by zero or more non-@ characters in front of it.

like image 36
Sergey Kalinichenko Avatar answered Sep 25 '22 00:09

Sergey Kalinichenko