Pattern matching email address using regular expressions [duplicate]
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:
FirstName.LastName@gmail.com
I want to be sure that there is a period somewhere before the '@' character and that the characters after the '@开发者_如何转开发' character matches gmail.com
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
check valid email
^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9.+!%-]{1,64}|)|\"[a-zA-Z0-9.+!% -]{1,64}\")@[a-zA-Z0-9][a-zA-Z0-9.-]+(.[a-z]{2,}|.[0-9]{1,})$
enforced rules:
- must start with alphanumeric char
- can only have alphanumeric and @._-% char
- cannot have 2 consecutives . exept for quoted string
- char before @ can only be alphanumeric and ._-%, exept for quoted string
- must have @ in the middle
- need to have at least 1 . in the domain part
- cannot have double - in the domain part
- can only have alphanumeric and .- char in the domain part
- need to finish by a valid extension of 2 or more letters
- support IP address (test@1.1.1.1)
- support for quoted user name
You don't even need regex since your requirements are pretty specific. Not sure what language you're using, but most would support doing a split on @
and checking for a .
. In python:
name, _, domain = email.partition('@')
if '.' in name and domain == 'gmail.com':
# valid
You haven't tell us what kind of regex flavor you need however this example will fit most of them:
.*\..*@gmail.com
Assuming Unix style where .
is any character: .*\..*@gmail\.com
Edit: escaped the .
in the domain
I used follwing regex expression to validate the email address. Also I have added a small code snippet in C# language regarding to that.
Regex - "^[a-zA-Z0-9]{3,20}@gmail.com$"
Code :-
static void Main(string[] args)
{
Console.WriteLine("Enter Your Text ");
string input = Console.ReadLine();
Console.WriteLine("The Text that you have entered is :" + input);
Console.ReadLine();
string pattern = "^[a-zA-Z0-9]{3,20}@gmail.com$";
Regex regex = new Regex(pattern);
bool results = regex.IsMatch(input);
Console.WriteLine(results.ToString());
Console.ReadLine();
}
Emails such as ∂øøµ$∂å¥!@gmail.com also checked and show as false here. ;-)
精彩评论