public class MatchesTest {
public static void main(String[] args){
String[] mails={"123456@qq.com","1234567@126.com","12345678156.com"};
String mailRegEx="\\w{3,20}@\\w+\\.(cn|com|org\net|gov)";
Pattern p=Pattern.compile(mailRegEx);
Matcher matcher =null;
for(String mail:mails)
{
if(matcher==null)
matcher=p.matcher(mail);
else
matcher.reset(mail); //为什么要加判断和reset()方法。直接输出p.matcher(mail);也可以得到想要的结果啊?????????
String result=mail+(matcher.matches()? "是":"不是")+"一个有效的邮箱";
System.out.println(result);
}
}
}
|
|