import java.sql.*; 
import java.io.*; 
import java.util.regex.*; public class test{ 
public static void main(String[] args){ 
try{ 
String s = ""; 
while(!s.equals("q")){ 
System.out.print("input:"); 
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in)); 
s = in.readLine(); 
System.out.println("your input is :"+s); 
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 
Pattern regex = Pattern.compile(check); 
Matcher matcher = regex.matcher(s); 
boolean isMatched = matcher.matches(); 
if(isMatched){ 
System.out.println("it@#s a email"); 
}else{ 
System.out.println("it@#s not a email"); 
} } 
}catch(Exception e){ 
System.out.println("error"+e.getMessage()); 


}

解决方案 »

  1.   

    public static boolean isEmail(String str)
        {
            str = str.trim();
            String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
            return str.matches(regex);
        }
      

  2.   

    请问有简单的方法吗?
    比如用String类的charAt()和indexOf()方法查找@和.符号的位置!
    谢谢!麻烦了!messe
      

  3.   

    我上面那个方法是最简单的了。
            String regex = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
            "[email protected]".matches(regex);
      

  4.   

    charAt()
    indexOf()
    也能做,但是麻烦,也容易出错。上面的正则表达式已经是很成熟的东西了,为什么不用呢?
      

  5.   

    如果用indexOf判断,
    我给个思路吧,程序你自己写。(1)有且只有一个@
    (2)有且可以有多个.
    (3)@后只能有一个. 但是不能连着
    (4)@前不能为空
    (4)最后一个点后不能为空
      

  6.   

    String email = "[email protected]";int a = email.indexOf("@");
    int b = email.lastIndexOf("@");
    int c = email.indexOf("[.]");
    int c = email.lastIndexOf("[.]");
    if(a==0 || c==0 || a!=b || b==d-1 || d==email.length()-1)
    {
       // @或者.开头,或者有多个@, 或者@.连在一起,或者.结尾
       System.out.println("不合法");
    }
    else 
    {
      System.out.println("合法");
    }
      

  7.   

    这个我完善了一下:
      String email = "[email protected]";        int a = email.indexOf("@");
            int b = email.lastIndexOf("@");
            int c = email.indexOf(".");
            int d = email.lastIndexOf(".");
            if(a==0 || a==-1 || c==0 || c==-1 || a!=b || b==d-1 || d==email.length()-1)
            {
                // @或者.没有,@或者.开头,或者有多个@, 或者@.连在一起,或者.结尾
                System.out.println("不合法");
            }
            else 
            {
                System.out.println("合法");
            }
    谢谢!结贴啦!
      

  8.   

    String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 
    本人现在还没学正则表达式,有哪位能帮具体解释下上面的语句吗?
    还有mather又是什么?