Regular Expression in Base R Regex to identify email address

末鹿安然 提交于 2019-11-29 12:25:02
42-
> "^[[:alnum:].-_]+@[[:alnum:].-]+$"->regex
> str_match(emails, regex)
     [,1]                   
[1,] "larry@gmail.com"      
[2,] "larry-sally@sally.com"
[3,] "larry@sally.larry.com"

The @-sign is not in need of escaping in regex. And "." and "-" are not special in character classes. If you want to add a requirement for ".com",".co", ".edu", ".org" then you should specify how complete that list needs to be.

As pointed out by M42, this is not a surefire method. In fact it is claimed that there is no sure-fire method: Using a regular expression to validate an email address

I found this regex worked better for me:

^[[:alnum:]._-]+@[[:alnum:].-]+$

Dash does have a special meaning in a character class unless it is the last character. It is a range operator, as in "A-Z"

Actually, I'd recommend a longer regex, since the solutions above allow for an email like test@test.com. with a trailing dot.

isMail <- function(x){
   grepl("^[[:alnum:]._-]+@[[:alnum:].-]+$", x))
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!