e-mail Regular expressions in Java

Source: Internet
Author: User

When it comes to regular expressions, there are a lot of common expressions on the web, but in fact, the average person is not willing to go to study, that is, to take the direct use of the line. However, in fact, there may be times when the actual situation in the project or the company is different, we have to modify the regular expression, according This requires us to study the regular expression of some of the wording, take doctrine, let us enjoy a wealth of network sharing resources at the same time, also brought us the inertia, about the regular expression of some use, I do not introduce more, online has a lot of introduction. (some basic usage of http://blog.sina.com.cn/s/blog_4c925dca01009h1a.html regular expressions)

Some time ago, Antioch in the project will be the e-mail verification positive. Allow multiple emails to be entered ; To separate and then, when submitting in a page form, whether it is a single message or multiple messages, is correct. About multiple messages, or individual messages I don't say much, it's multiple, just use split (";") To generate an array, and then loop through each email to determine whether the correct email format is OK. This is not the point that Antioch to talk about, I just want to explain here, I want to check the situation here.

General email, like [email protected],[email protected] such a number of commonly used forms, but in our company some customers in the mailbox but some [email Protected],[email protected],[email protected] This kind of similar form, before the @ symbol is a bit., the original is to use, but not now, You have to study the usage of the regular.

Friends, if there is a regular expression of the problem, welcome to Exchange discussion in this message!

The original regular expression

/^[a-za-z0-9_-][email protected][a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +$/;

I modified the regular expression according to my situation

/^ (\w) + (\.\w+) *@ (\w) + ((\.\w{2,3}) {1,3}) $/;

Or

/^ (\w) + (\.\w+) *@ (\w) + ((\.\w+) +) $/;

Character Description:
^: matches the start position of the input.
\: Marks the next character as a special character or literal.
*: Matches the previous character 0 or more times.
+: Matches the previous character one or more times.
(pattern) matches the pattern and remembers the match.
X|y: Matches x or Y.
[A-Z]: represents a range of characters. Matches any character within the specified range.
\w: Matches any word character, including underscores.

{n,m} matches at least n times and matches up to M
$: Matches the end of the input.

Attached to a simple JS


function Checkemail ()
{
var emailvalue=document. Getelementbyid_r ("email"). Value;
if (!isemail (Emailvalue))
{
Alert ("You entered the wrong mailbox, please re-check and then enter!");
Document. Getelementbyid_r ("email"). focus ();
return false;
}
return true;
}

function Isemail (str) {
var reg =/^ (\w) + (\.\w+) *@ (\w) + ((\.\w+) +) $/;
return Reg.test (str);
}

Antioch also attaches a positive method for entering multiple messages

Verify that the mailbox format is correct 20080602_heyitang
var email=document. Getelementbyid_r ("Trans_email"). Value;
If the user has entered the mailbox to be judged

if (email!=null)
{if (Email.indexof (";") ==-1)
{
if (!isemail (email))
{
Alert ("The individual message format you entered is incorrect, please re-check and then enter");
Document. Getelementbyid_r ("Trans_email"). focus ();
return false;
}
}
Else
{
var emailarray=email.split (";");
for (i=0;i<emailarray.length;i++)
{
Here to prevent the appearance[email protected];[email protected];; This time, there may be elements in the array that have no content
if (Emailarray[i]!=null | | emailarray[i]!= "")
{
if (!isemail (Emailarray[i]))
{
Alert ("You have entered more than one mailbox format, the mailbox format is not correct, please re-check and then enter");
Document. Getelementbyid_r ("Trans_email"). focus ();
return false;
}
}
}
}
}

In Java, it is:

String regex = "\\w+ (\\.\\w) *@\\w+ (\\.\\w{2,3}) {1,3}";
String[] str1 = {"[email protected]", "[email protected]", "[email protected]", "[email protected]", "Han. @sohu. com.cn", " [Email protected] "};
for (String str:str1) {
System.out.println (str+ "\\\\. "+str.matches (regex));
}


Reference: http://www.cnblogs.com/vs-bug/archive/2010/03/26/1696752.html

e-mail Regular expressions in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.