I have a large list of emails I am running through. A lot of the emails have typos. I am trying to build a string that will check valid emails.
this is what I have
I guess the example from the book can be improved to match emails with -
in subdomain.
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
For example:
> 'some@email.with-subdomain.com' =~ VALID_EMAIL_REGEX
=> 0
try this!!!
/\[A-Z0-9._%+-\]+@\[A-Z0-9.-\]+\.\[AZ\]{2,4}/i
only email string selected
"Robert Donhan" <bob@email.com>sadfadf
Robert Donhan <bob@email.com>
"Robert Donhan" abc.bob@email.comasdfadf
Robert Donhan bob@email.comadfd
Yours is complicated indeed.
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
The above code should suffice.
Explanation of each piece of the expression above for clarification:
Start of regex:
/
Match the start of a string:
\A
At least one word character, plus, hyphen, or dot:
[\w+\-.]+
A literal "at sign":
@
A literal dot:
\.
At least one letter:
[a-z]+
Match the end of a string:
\z
End of regex:
/
Case insensitive:
i
Putting it back together again:
/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
Check out Rubular to conveniently test your expressions as you write them.
This works good for me:
if email.match?('[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})')
puts 'matches!'
else
puts 'it doesn\'t match!'
end
Here's a great article by David Celis explaining why every single regular expression you can find for validating email addresses is wrong, including the ones above posted by Mike.
From the article:
The local string (the part of the email address that comes before the @) can contain the following characters:
`! $ & * - = ` ^ | ~ # % ' + / ? _ { }`
But guess what? You can use pretty much any character you want if you escape it by surrounding it in quotes. For example, "Look at all these spaces!"@example.com is a valid email address. Nice.
If you need to do a basic check, the best regular expression is simply /@/
.
This has been built into the standard library since at least 2.2.1
URI::MailTo::EMAIL_REGEXP