R gsub to extract emails from text

女生的网名这么多〃 提交于 2019-12-06 06:48:36

We can try the str_extract() from stringr package:

str_extract(text, "\\S*@\\S*")

[1] "Saolonm@hotmail.com"              
[2] "26.leonard@gmail.com"             
[3] "jcdavola31@gmail.com"             
[4] "andrescarnederes@headset.cl"      
[5] "luciana.chavela.ecuador@gmail.com"

where \\S* match any number of non-space character.

From the answer you posted in your question,

library(stringr)
str_extract(a, '\\S+@\\S+|\\{(?:\\w+, *)+\\w+\\}@[\\w.-]+')
#[1] "Saolonm@hotmail.com"               "26.leonard@gmail.com"              "jcdavola31@gmail.com"              "andrescarnederes@headset.cl"      
#[5] "luciana.chavela.ecuador@gmail.com"

We can use base R options to do this

unlist(regmatches(a, gregexpr("\\S+@\\S+", a)))
#[1] "Saolonm@hotmail.com"    
#[2]"26.leonard@gmail.com" 
#[3] "jcdavola31@gmail.com"             
#[4] "andrescarnederes@headset.cl"
#[5] "luciana.chavela.ecuador@gmail.com"

Or as the OP's post is about a solution with gsub/sub

sub("(.*\\s+|^)(\\S+@\\S+).*", "\\2", a)
#[1] "Saolonm@hotmail.com" 
#[2] "26.leonard@gmail.com" 
#[3] "jcdavola31@gmail.com"             
#[4] "andrescarnederes@headset.cl"  
#[5] "luciana.chavela.ecuador@gmail.com"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!