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)