Adding simple e-mail validation to my code, I created the following function:
在我的代码中添加简单的电子邮件验证,我创建了以下函数:
def isValid(email: String): Boolean = if("""(?=[^\s]+)(?=(\w+)@([\w\.]+))""".r.findFirstIn(email) == None)false else true
This will pass emails like bob@testmymail.com
and fail mails like bobtestmymail.com
, but mails with space characters slip through, like bob @testmymail
will also return true.
这将传递像bob@testmymail.com这样的电子邮件和bobtestmymail.com之类的邮件失败邮件,但是带有空格字符的邮件会漏掉,比如bob @testmymail也会返回true。
I'm probably being silly here...
我可能在这里很傻......
3 个解决方案
#1
4
As I've tested your regex and it was catching simple emails, I then checked your code and saw that you're using findFirstIn. I believe that is your problem. findFirstIn will jump all the spaces until it matches some sequence anywhere in the string. I believe that in your case it's better to use unapplySeq and check if it returns Some List
当我测试你的正则表达式并且正在捕捉简单的电子邮件时,我检查了你的代码,发现你正在使用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
#2
15
My function is inspired from the one that the Play Framework uses (see PlayFramework) and uses the regexp presented here: W3C recommendation. Hope it helps. All tests suggested in the other questions are passed.
我的功能灵感来自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
}
#3
1
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
#1
4
As I've tested your regex and it was catching simple emails, I then checked your code and saw that you're using findFirstIn. I believe that is your problem. findFirstIn will jump all the spaces until it matches some sequence anywhere in the string. I believe that in your case it's better to use unapplySeq and check if it returns Some List
当我测试你的正则表达式并且正在捕捉简单的电子邮件时,我检查了你的代码,发现你正在使用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
#2
15
My function is inspired from the one that the Play Framework uses (see PlayFramework) and uses the regexp presented here: W3C recommendation. Hope it helps. All tests suggested in the other questions are passed.
我的功能灵感来自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
}
#3
1
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