5

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:

first.last@example.com
first.middle.last@example.com

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

first.last(comment)@example.com
leopik
  • 2,323
  • 2
  • 17
  • 29
  • 3
    Do yourself a favor and use the search or a web search engine. Type in "email regex" and use another way of validation. – CodeCaster May 06 '15 at 12:16
  • I'm sorry, but I don't think you read the question. I'm not trying to validate emails. I already have a set of emails that are valid, I just need to select ones, that contain dot before `@` sign. And yes, I have used google before asking. – leopik May 06 '15 at 12:18
  • I tried your regex in a tester and it looks like it is doing what you want. When does it fail ? – Bongo May 06 '15 at 12:20
  • 3
    So then give some example input and output. What email addresses do you want to match, why doesn't this regex help there and what have you tried to make it match? – CodeCaster May 06 '15 at 12:20
  • ^[a-zA-Z0-9()\.']{1,}\.[a-zA-Z0-90()\.']{1,}@example\.com$ this will match the example you gave – Bongo May 06 '15 at 12:26
  • Just FYI: this regex will only match emails with a dot before the `@` sign: `^(?=[^@]+\.@)[a-zA-Z\.']{1,}\.[a-zA-Z\.']{1,}@example\.com$`. See https://regex101.com/r/cN7lH2/1. – Wiktor Stribiżew May 06 '15 at 12:26

5 Answers5

7

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

James
  • 80,725
  • 18
  • 167
  • 237
  • Something like `.Where(x => {int i = x.IndexOf("."); return i > 0 &&i < x.IndexOf("@");})` would be better I guess .... – Markus May 06 '15 at 12:32
  • 1
    @Markus yep, although I'd argue having a named delegate is better from a readability perspective. `Where(dotBeforeAt)` just looks much better :) – James May 06 '15 at 12:33
  • There are multiple good, but this one seems to be one that I'll use. Thanks again! – leopik May 06 '15 at 12:38
  • @Bongo this isn't an email validator, it's a filter. The OP just wants all email addresses that contain a `.` before the `@` symbol - this code does that. The OP has already pre-validated the emails beforehand. – James May 06 '15 at 12:47
  • @James okay true. haven't said anything :) – Bongo May 06 '15 at 12:50
3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    I'd use `[.](?=[^@]*@)` myself... :p (in seriousness, you don't even need lookaround, just `[.].*@` ) – Rawling May 06 '15 at 12:22
2

You can use lookahead.. for this purpose..

(?=\.).+@

Explanation:

  • Look forward for a dot, followed by any characters followed by @

Edit: To match the email with the above criteria.. you can use

.+(?=\..+@).+

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
2

LinQ could be used to both avoid a regex and to avoid calling IndexOf() twice on ".":

var dottyEmails = (from email in emails 
                   let dotIndex = email.IndexOf(".")
                   let atIndex = email.IndexOf("@")
                   where dotIndex >= 0 && dotIndex < atIndex
                   select email).ToList();
hangy
  • 10,765
  • 6
  • 43
  • 63
David Arno
  • 42,717
  • 16
  • 86
  • 131
2

James' answer will probably give you the best performance. However, the IndexOf approach will not handle a quoted-string (ie. "abc@.xtest.yz"@example.com, which is a valid address according to RCF 5322). To support that case, and if performance is not an issue, you could also use the following, which is a little bit more readable and verbose on what the intention of the LINQ query is:

emails.Select(m => new MailAddress(m)).Where(m => m.User.Contains('.')).ToList();

The overhead of building the MailAddress objects is pretty obvious, but this makes it really clear that you want those addresses that have a dot in the local part of the address.

Community
  • 1
  • 1
hangy
  • 10,765
  • 6
  • 43
  • 63