代码之家  ›  专栏  ›  技术社区  ›  Rohit Agarwal

regex问题:ismatch方法从不返回

  •  0
  • Rohit Agarwal  · 技术社区  · 14 年前

    我有一个正则表达式问题需要帮助。它试图验证电子邮件地址。

    Regex rx = new Regex(@"^[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\@[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\.[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*$|^$");
    rx.IsMatch("john.gilbert.stu.seattle.washington.us"); 
    

    这个 等匹配 方法从不返回该特定字符串。它进入一个无限循环。有人能看看这个模式有什么问题吗?

    谢谢!

    3 回复  |  直到 14 年前
        1
  •  0
  •   Jason Quinn    14 年前

    第一个“+”是问题所在,如果您删除它,它就会运行

        2
  •  1
  •   Matt Connolly    14 年前

    (有些东西)不好。请参见这里: http://www.regular-expressions.info/catastrophic.html

        3
  •  0
  •   Darin Dimitrov    14 年前

    我建议您使用以下方法验证电子邮件地址:

    try
    {
        MailAddress addr = new MailAddress("foo@bar.com");
    }
    catch (FormatException exc)
    {
        // The email address is not valid
    }
    

    这里有一个 interesting read .