Java Regular Expressions (email match)

Source: Internet
Author: User

Turn http://blog.csdn.net/marila4720/article/details/8728840

Package URL;
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;

public class test1{
public static void Main (String[]args) {
String regex = "\ \[email protected]\\w+\\. (com\\.cn) |\\[email protected]\\w+\\. (COM|CN) ";
/*
\w representative [a-za-z0-9_], the reason is written \\w because \ has escaped meaning, so to output \ must be written \ \
+ representative can have one or more times
That's why, because. Represents an arbitrary character in a regular expression, so you want to write it. You need to use the escape character \
To output \ You have to write \ \
() represents a combination, for example (COM) to match a string containing COM, and COM to match a C or O or M
The string
| Represent this or
Note: The regular expression to match the time is a priority, from left to right, () can also be seen as a
Priority control, \ \[email protected]\\w+\\. (COM|CN) + change to \ \[email protected]\\w+\\.com|cn+ program will think \ \[email protected]\\w+\\.com or CN
So when the[email protected]The following program will output CN when matching, and our intention is to let COM and CN or
*/


String regex = "[A-za-z0-9_]+[@][a-za-z0-9]+ ([\\.com]|[ \\.cn]) ";
Pattern pattern = pattern.compile (regex);
String context = "[email protected] ; [Email protected]; [Email protected] "
+"; [Email protected]; [email protected] ";
Matcher Matcher = pattern.matcher (context);
while (Matcher.find ()) {
System.out.println (Matcher.group ());

}

}

Java Regular Expressions (email match)

Related Article

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.