package URL;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test1{
public static void main(String[]args){
String regex = "\\w+@\\w+\\.(com\\.cn)|\\w+@\\w+\\.(com|cn)";
/*
\w代表[a-zA-z0-9_],之所以写成\\w是因为\有转义的意思,所以要输出 \必须得写成\\
+ 代表可以有一次或者多次
\\.就代表着.,因为.在正则表达式中代表一个任意的字符,所以要想写出.来就得要用到转义字符\
要输出\就得要写成\\
()代表着一个组合,例如(com)会去匹配含有com的字符串,而com会去匹配含有c或者o或者m
的字符串
| 代表这或
注意:按照正则表达式去匹配的时候是有优先级的,从左至右,()也可以看做是一
优先级的控制,\\w+@\\w+\\.(com|cn)+ 改为\\w+@\\w+\\.com|cn+程序会以为\\w+@\\w+\\.com或者cn
这样当对[email protected]匹配时下面的程序会输出cn,而我们的初衷是让com与cn或
*/
//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());
}
}