0

I have a line of text (string) which contain some email addresses. I know there are like a million posts on email regex, but in this question my definition of email address is just a string of characters with no space in between and contains exactly one '@' symbol.

e.g. Lazy dog jump over the fence@quick brown fox's house. And when I need a sample text_I/just@randomly.think of words to type.

This will give me fence@quick and text_I/just@randomly.think as a valid match.

I tried new Regex(@"\b.*@.*\b") but it matches the entire line of text, although that claims to be the word boundary meta character?

I actually got it to work with new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*") but is there a simpler way? EDIT: This still misses some invalid email address.

Jake
  • 11,273
  • 21
  • 90
  • 147

1 Answers1

0

You should use this regex

\S+@\S+

\S would match any character that is not space

But even space is a valid character in email.so use it cautiously..


Have a look at these articles

Bottom line is these standards have never been followed so its better to stick with ^[^@]+@[^@]+$

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • I know this said "naively", but just out of curiosity, can you have an email without a dot after the `@`? – Alxandr Jul 09 '13 at 07:24
  • @Alxandr as specified in the docs: the domain must match the requirements for a hostname, consisting of letters, digits, hyphens and **dots** – Anirudha Jul 09 '13 at 07:32
  • @Alxandr see it [here](http://en.wikipedia.org/wiki/Email_address#Domain_part) also refer to above links – Anirudha Jul 09 '13 at 07:32
  • @Alxandr well it seems to be optional cause `admin@mailserver1` is valid emailid – Anirudha Jul 09 '13 at 07:41
  • `postbox@com (top-level domains are valid hostnames)`... WOW o.O – Alxandr Jul 09 '13 at 10:36