日期:2014-05-20 浏览次数:20705 次
import java.util.regex.*;
public class TestMatches {
public static void main(String[] args) {
// TODO Auto-generated method stub
String []mails={"zpcandzhj@163.com","ssw@126","zp2ss@njue.cn"};
String mailRegEx="\\w{1,20}@\\w+\\.(com|cn|org|net|gov)";
Pattern mailPattern=Pattern.compile(mailRegEx);
Matcher matcher=null;
for(String mail:mails){
if(matcher==null){
matcher =mailPattern.matcher(mailRegEx);
}else{
matcher.reset(mail);
}
if(matcher.matches()){
System.out.println(mail+"是个有效的邮件地址!");
}else{
System.out.println(mail+"不是个有效的邮件地址!");
}
}
}
}
for (String mail : mails)
{
matcher = mailPattern.matcher(mail);
if (matcher.matches())
{
System.out.println(mail + "是个有效的邮件地址!");
}
else
{
System.out.println(mail + "不是个有效的邮件地址!");