11

在我的代码中添加简单的电子邮件验证,我创建了以下函数:

def isValid(email: String): Boolean = if("""(?=[^\s]+)(?=(\w+)@([\w\.]+))""".r.findFirstIn(email) == None)false else true

这将通过邮件 likebob@testmymail.com和失败邮件 like bobtestmymail.com,但是带有空格字符的邮件会漏掉, likebob @testmymail也会返回 true。

我可能在这里很傻......

4

5 回答 5

25

我的函数灵感来自 Play Framework 使用的函数(请参阅PlayFramework)并使用此处介绍的正则表达式:W3C 推荐。希望能帮助到你。其他问题中建议的所有测试均已通过。

private val emailRegex = """^[a-zA-Z0-9\.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$""".r


def check(e: String): Boolean = e match{
    case null                                           => false
    case e if e.trim.isEmpty                            => false
    case e if emailRegex.findFirstMatchIn(e).isDefined  => true
    case _                                              => false
}
于 2015-09-07T20:04:01.177 回答
4

由于我测试了您的正则表达式并且它正在捕获简单的电子邮件,然后我检查了您的代码并看到您正在使用 findFirstIn。我相信这是你的问题。findFirstIn 将跳过所有空格,直到它匹配字符串中任何位置的某个序列。我相信在您的情况下,最好使用 unapplySeq 并检查它是否返回 Some List

def isValid(email: String): Boolean =
   if("""(?=[^\s]+)(?=(\w+)@([\w\.]+))""".r.findFirstIn(email) == None)false else true

def isValid2(email: String): Boolean =
  """(\w+)@([\w\.]+)""".r.unapplySeq(email).isDefined

isValid("test@gmail.com")                        //> res0: Boolean = true

isValid("t es t@gmailcom")                       //> res1: Boolean = true

isValid("b ob @tes tmai l.com")                  //> res2: Boolean = false

isValid2("test@gmail.com")                       //> res3: Boolean = true

isValid2("t es t@gmailcom")                      //> res4: Boolean = false

isValid2("b ob @tes tmai l.com")                 //> res5: Boolean = false

// but those don't work for both:
// I recommend you using a proper regex pattern to match emails
isValid("test@gma.i.l.c.o.m")                    //> res6: Boolean = true

isValid("test@gmailcom")                         //> res7: Boolean = true

isValid2("test@gma.i.l.c.o.m")                   //> res8: Boolean = true

isValid2("test@gmailcom")                        //> res9: Boolean = true
于 2012-12-17T14:13:55.977 回答
2
scala> def isValid(email : String): Boolean = if("""^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@([a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$""".r.findFirstIn(email) == None)false else true
v: (email: String)Boolean

scala> isValid("""bob@test.com""")
res0: Boolean = true

scala> isValid("""bob @test.com""")
res1: Boolean = false

scala> isValid("""bobtest.com""")  
res2: Boolean = false
于 2012-12-17T13:08:54.823 回答
0
scala> val email4 = """([\w\.!#$%&*+/=?^_`{|}~-]+)@([\w]+)([\.]{1}[\w]+)+""".r
email4: scala.util.matching.Regex = ([\w\.!#$%&*+/=?^_`{|}~-]+)@([\w]+)([\.]{1}[\w]+)+

scala> email4.pattern.matcher("arun#eja-729@gmail..gov").matches
res10: Boolean = false

scala> email4.pattern.matcher("arun#eja-729@gmail.com.gov").matches
res11: Boolean = true

scala> email4.pattern.matcher("arun#eja-729@gmail.com.gov.in").matches
res12: Boolean = true

scala> email4.pattern.matcher("arun#eja-729@gmail.com.gov.in.").matches
res13: Boolean = false

scala> email4.pattern.matcher("arun#eja-729@gmail.com.").matches
res14: Boolean = false

scala> email4.pattern.matcher("arun#eja-729@gmail.com").matches
res15: Boolean = true

scala> email4.pattern.matcher("arun#eja-729@gmail..com").matches
res16: Boolean = false

scala> email4.pattern.matcher("arun#eja-729@gmail.ap.com").matches
res17: Boolean = true

scala> email4.pattern.matcher("arun#eja-729@gmail.ap.com.").matches
res18: Boolean = false
于 2020-02-04T04:42:37.173 回答
0

下面是电子邮件 id 的正则表达式,最小 10 和最大 30 字符长度。

scala> val email4 = """[([\w\.!#$%&*+/=?^_`{|}~-]+)@([\w]+)([\.]{1}[\w]+)+]{10,30}""".r
email4: scala.util.matching.Regex = [([\w\.!#$%&*+/=?^_`{|}~-]+)@([\w]+)([\.]{1}[\w]+)+]{10,30}

scala> email4.pattern.matcher("ar@g.com").matches
res19: Boolean = false

scala> email4.pattern.matcher("ar@gmail.com").matches
res20: Boolean = true

scala> email4.pattern.matcher("ar1234567890@gmail1234567890.com").matches
res21: Boolean = false

scala> email4.pattern.matcher("ar1234567890@gmail123456780.com").matches
res22: Boolean = false

scala> email4.pattern.matcher("ar1234567890@gma.com").matches
res23: Boolean = true

scala> email4.pattern.matcher("ar1234567890@gmghfjdfcga.com").matches
res24: Boolean = true

scala> email4.pattern.matcher("ar1234567890@gmghfjdfcga1.com").matches
res25: Boolean = true

scala> email4.pattern.matcher("ar1234567890@gmghfjdfcga111.com").matches
res26: Boolean = false

scala> email4.pattern.matcher("ar1234567890@gmghfjdfcga11.com").matches
res27: Boolean = true
于 2020-02-04T04:47:20.660 回答