Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正则表达式电子邮件验证_Java_Regex_Count - Fatal编程技术网

Java 正则表达式电子邮件验证

Java 正则表达式电子邮件验证,java,regex,count,Java,Regex,Count,这是我之前文章的延续,我的代码: public class Main { static String theFile = "C:\\Users\\Pc\\Desktop\\textfile.txt"; public static boolean validate(String input) { boolean status = false; String REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"

这是我之前文章的延续,我的代码:

public class Main {

static String theFile = "C:\\Users\\Pc\\Desktop\\textfile.txt";

public static boolean validate(String input) {
    boolean status = false;
    String REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    Pattern pattern = Pattern.compile(REGEX);
    Matcher matcher = pattern.matcher(input);
    if (matcher.matches()) {
        status = true;
    } else {
        status = false;
    }
    return status;
}

public static void main(String[] args) {
    BufferedReader br = null;

    try {
        br = new BufferedReader(new FileReader(theFile));
        String line;
        int count = 0;
        while ((line = br.readLine()) != null) {
            String[] arr = line.split("#");
            for (int x = 0; x < arr.length; x++) {
                if (arr[x].equals(validate(theFile))) {
                    count++;
                }
                System.out.println("no of matches " + count);

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    Main.validate(theFile);
}

}
公共类主{
静态字符串theFile=“C:\\Users\\Pc\\Desktop\\textfile.txt”;
公共静态布尔验证(字符串输入){
布尔状态=假;
String REGEX=“^[\u A-Za-z0-9-\\+]+(\\.[u A-Za-z0-9-]+)*@”
+“[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$”;
Pattern=Pattern.compile(REGEX);
Matcher Matcher=pattern.Matcher(输入);
if(matcher.matches()){
状态=真;
}否则{
状态=假;
}
返回状态;
}
公共静态void main(字符串[]args){
BufferedReader br=null;
试一试{
br=新的BufferedReader(新文件读取器(theFile));
弦线;
整数计数=0;
而((line=br.readLine())!=null){
字符串[]arr=line.split(“#”);
对于(int x=0;x
结果表明: 匹配项0的数目 匹配项0的数目 匹配项0的数目 匹配项0的数目

这是我输入文件中的文本 sjbfbhbs@yahoo.com # fgfgfgf@yahoo.com # ghghgh@gamil.com #fhfbs@y.com

我的输出应该是三封电子邮件,因为最后一个字符串不是标准的电子邮件格式
我知道我不想通过
(arr[x].validate(theFile))
您的代码中有几个错误:

  • if(arr[x].equals(validate(theFile))
    检查邮件地址字符串是否等于从
    validate()
    方法获得的布尔值。这种情况永远不会发生
  • validate()
  • 拆分输入字符串(行)后,会留下空白,因为您只在
    #
    符号处拆分。您可以在
    trim()
    之后删除每个字符串(请参见下面的代码),也可以在
    \\s*\35;\\ s*
    split()
    而不仅仅是
  • 下面是一个包含所有修复程序的示例(我省略了读取文件的部分,而是使用了包含邮件地址的字符串!):


    编辑:如果模式不应该与最后一个邮件地址匹配,则必须更改模式。现在,它与所有这些都匹配。

    我一直使用这个:

        public static bool Validate(string email)
        {
            string expressions = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
            return Regex.IsMatch(email, expressions);
        }
    
    注意:我还有一个函数,它可以在字符串中有多个
    @
    符号时“清理”字符串

    编辑:以下是我如何清除多余的
    @
    符号。注意这将保留它找到的第一个
    @
    ,只删除其余的。在对其运行验证函数之前,应先使用此函数

        public static string CleanEmail(string input)
        {
            string output = "";
    
            try
            {
                if (input.Length > 0)
                {
                    string first_pass = Regex.Replace(input, @"[^\w\.@-]", "");
                    List<string> second_pass = new List<string>();
                    string third_pass = first_pass;
                    string final_pass = "";
    
                    if (first_pass.Contains("@"))
                    {
                        second_pass = first_pass.Split('@').ToList();
    
                        if (second_pass.Count >= 2)
                        {
                            string second_pass_0 = second_pass[0];
                            string second_pass_1 = second_pass[1];
    
                            third_pass = second_pass_0 + "@" + second_pass_1;
    
                            second_pass.Remove(second_pass_0);
                            second_pass.Remove(second_pass_1);
                        }
                    }
    
                    if (second_pass.Count > 0)
                    {
                        final_pass = third_pass + string.Join("", second_pass.ToArray());
                    }
                    else
                    {
                        final_pass = third_pass;
                    }
    
                    output = final_pass;
    
                }
            }
            catch (Exception Ex)
            {
    
            }
    
            return output;
        }
    
    公共静态字符串清理电子邮件(字符串输入)
    {
    字符串输出=”;
    尝试
    {
    如果(input.Length>0)
    {
    字符串first_pass=Regex.Replace(输入,@“[^\w\.@-]”,“”);
    列表第二次通过=新列表();
    字符串第三遍=第一遍;
    字符串final_pass=“”;
    如果(第一次通过包含(“@”))
    {
    第二遍=第一遍分割('@').ToList();
    如果(第二次通过计数>=2)
    {
    字符串second_pass_0=second_pass[0];
    字符串second_pass_1=second_pass[1];
    第三遍=第二遍0+“@”+第二遍1;
    第二次通过。删除(第二次通过0);
    第二次通过。移除(第二次通过1);
    }
    }
    如果(第二次通过计数>0)
    {
    final_pass=third_pass+string.Join(“,second_pass.ToArray());
    }
    其他的
    {
    最终通过=第三次通过;
    }
    输出=最终通过;
    }
    }
    捕获(例外情况除外)
    {
    }
    返回输出;
    }
    
    如果我的答案解决了您的问题,您可以将其标记为可供其他人使用和我的无用声望积分:)功能是什么?interesting@valentine下午好,怎么办?我是新来的@David Bentley有模拟mvc的经验吗?控制器测试弹簧boot@valentine不。我编写RDP软件、数据库管理软件、windows服务和手机应用程序。在编写电子邮件验证程序之前,请先看看这些
        public static bool Validate(string email)
        {
            string expressions = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
            return Regex.IsMatch(email, expressions);
        }
    
        public static string CleanEmail(string input)
        {
            string output = "";
    
            try
            {
                if (input.Length > 0)
                {
                    string first_pass = Regex.Replace(input, @"[^\w\.@-]", "");
                    List<string> second_pass = new List<string>();
                    string third_pass = first_pass;
                    string final_pass = "";
    
                    if (first_pass.Contains("@"))
                    {
                        second_pass = first_pass.Split('@').ToList();
    
                        if (second_pass.Count >= 2)
                        {
                            string second_pass_0 = second_pass[0];
                            string second_pass_1 = second_pass[1];
    
                            third_pass = second_pass_0 + "@" + second_pass_1;
    
                            second_pass.Remove(second_pass_0);
                            second_pass.Remove(second_pass_1);
                        }
                    }
    
                    if (second_pass.Count > 0)
                    {
                        final_pass = third_pass + string.Join("", second_pass.ToArray());
                    }
                    else
                    {
                        final_pass = third_pass;
                    }
    
                    output = final_pass;
    
                }
            }
            catch (Exception Ex)
            {
    
            }
    
            return output;
        }