Pattern matching email address using regular expressions

Brad picture Brad · May 2, 2011 · Viewed 12.2k times · Source

Filter email address with regular expressions: I am new to regular expressions and was hoping someone might be able to help out.

I am trying to pattern match an email address string with the following format:

[email protected]

I want to be sure that there is a period somewhere before the '@' character and that the characters after the '@' character matches gmail.com

Answer

Alexey Kukanov picture Alexey Kukanov · May 2, 2011

You want some symbols before and after the dot, so I would suggest .+\..+@gmail\.com.

.+ means any symbols (.) can appear 1 or more times (+)
\. means the dot symbol; screened with backslash to suppress the special meaning of .
@gmail and com should be matched exactly.

See also Regular Expression Basic Syntax Reference

EDIT: gmail rules for account name only allow latin letters, digits, and dots, so a better regex is
[a-zA-Z0-9]+\.[a-zA-Z0-9]+@gmail\.com