1

I've tried the following code, but it gives me nomatch.

re:run("qw@qc.com", "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b").

regexp i got here http://www.regular-expressions.info/email.html

EDITED: Next doesnt work to

re:run("345345", "\b[0-9]+\b").

If you got just en email in string when that one will match

re:run("qw@qc.com", "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$").
Yola
  • 18,496
  • 11
  • 65
  • 106

2 Answers2

5

I hesitate to answer this question, since I believe it relies on an incorrect assumption - that you can determine whether an email address is valid or not with a regular expression. See this question for more details; from a short glance I'd note that the regexp in your question doesn't accept the .museum and .рф top-level domains.

That said, you need to escape the backslashes. You want the string to contain backslashes, but in Erlang, backslashes are used inside strings to escape various characters, so any literal backslash needs to be written as \\. Try this:

3> re:run("qw@qc.com", "\\b[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}\\b").
{match,[{0,9}]}

Or even better, this:

8> re:run("qw@qc.com", "\\b[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*\\b").
{match,[{0,9}]}

That's the regexp used in the HTML 5 standard, modified to use \\b instead of ^ and $.

Community
  • 1
  • 1
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 2
    +1 but you should still extend this to allow e.g. `=` in the localpart, and note that any attempt using regex will fail on the thornier corner cases. Adding `-` to the domain part would at least allow the punycode representation of internationalized domain names (you should drop the length constraint entirely, of course). Moreover, you should allow arbitrarily deep subdomains -- many ccTLDs will not even allow a domain name with only two parts. – tripleee Jul 30 '12 at 16:11
0

Looks like you need a case-insensitive match ?

Currently [A-Z0-9._%+-] (for example) only matches upper-case characters (plus numbers etc).

One solution is to specify [A-Za-z]. Another solution is to convert your email address to uppercase prior to matching.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • ''re:run("qw@qw.qw", "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b").'' - gives me nomatch(( – Yola Jul 30 '12 at 10:06