Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- To revise the funciton to delete the global variable in R function?
- word <- c('abc noboby@stat.berkeley.edu','text with no email','first me@mything.com also you@yourspace.com')
- pattern <- '[-A-Za-z0-9_.%]+@[-A-Za-z0-9_.%]+\.[A-Za-z]+'
- getmail<-function(pattern,word){
- mail<<-c()
- sapply(word,function(x){
- out<-gregexpr(pattern,x)
- for (i in 1:length(out[[1]])){
- if (out[[1]][i]>0)
- mail<<-union(mail,substr(x,start=out[[1]][i],stop=out[[1]][i]+attr(out[[1]],"match.length")[i]-1))
- }})
- return(mail)
- }
- getmail(pattern,word)
- [1] "noboby@stat.berkeley.edu" "me@mything.com" "you@yourspace.com"
- ls()
- [1] "getmail" "mail" "pattern" "word"
- mail<-c()
- out<-gregexpr(pattern,word)
- for (i in 1:length(out)){
- for (j in 1:length(out[[i]])){
- if (out[[i]][j]>0)
- mail<-union(mail,substr(word[i],start=out[[i]][j],stop=out[[i]][j]+attr(out[[i]],"match.length")[j]-1))}}
- mail
- [1] "noboby@stat.berkeley.edu" "me@mything.com" "you@yourspace.com"
- > m <- gregexpr(pattern,word)
- > lapply(seq_along(word),
- function(i){substring(word[i],m[[i]],m[[i]] + attr(m[[i]],"match.length"))})
- [[1]]
- [1] "noboby@stat.berkeley.edu"
- [[2]]
- [1] ""
- [[3]]
- [1] "me@mything.com " "you@yourspace.com"
Add Comment
Please, Sign In to add comment