本文介绍了域名带有连字符时,电子邮件地址验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件地址验证正则表达式,该代码在这样的代码中使用:

I have an email address validation regex Which I use in the code like this:

public class Test {

  public static void main(String[] args) {
    try {
      String lineIwant = "[email protected]";
      String emailreg = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
      Boolean b = lineIwant.matches(emailreg);

      if (b == false) {
        System.out.println("Address is Invalid");
      }else if(b == true){
        System.out.println("Address is Valid");
      }
    } catch (Exception e) {

      e.printStackTrace();
      System.out.println(e.getMessage());
    }
  }
}

在此示例中的此特定电子邮件地址上,布尔值返回false,而这是有效的客户电子邮件地址.

On this specific email address in the example, the Boolean returns false while this is a valid customer email address.

我怀疑是因为 ramco group 之间的连字符,因为当我删除它时,布尔值返回true.

I am suspecting it is because of the hyphen between ramco and group because when I remove it the Boolean returns true.

如何更改我的正则表达式以容纳这样的电子邮件地址?

How can I change my regex to accommodate such an email address?

推荐答案

您的正则表达式不允许在 @ 符号后使用-,因此

Your regex is not allowing a - after the @ sign, so

String emailreg = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";

将修复"此特定问题.但是电子邮件地址比这复杂得多.使用正则表达式验证它们不是一个好主意.查看@DuncanJones的评论.

would "fix" this specific problem. But Email addresses are much more complicated than that. Validating them using a regex is not a good idea. Check out @DuncanJones' comment.

这篇关于域名带有连字符时,电子邮件地址验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:47