3

I have some email address where I am trying to extract the domain from. I found a solution here but it is taking too long.

I am trying with the following approach:

First remove all the text before the @ sign.

gsub("@(.+)$", "\\1", emails)

Other - not used

qdapRegex::ex_between(emails, ".", ".")

Data:

emails <- c("ut317@hotmail.com", "drrro@iueywapp.com", "esdfdsfos@lasdfsdfsdstores.com", 
"asfds@mobsdaff.com", "asfsdaf.gsdsfdsfd@hotmail.org", "asdfdsaf@sdffsddapp.com", 
"wqrerq.mwqerweing@mwerqwie.com", "qwera@niweqrerw.tv", "qwereqr3rew7@hotmail.com", 
"mqwerwewrk@moweqrewsfaslay.com")
user8959427
  • 2,027
  • 9
  • 20

3 Answers3

2

how about

sub(".*@(.*)\\..*","\\1",emails)

 [1] "hotmail"          "iueywapp"         "lasdfsdfsdstores" "mobsdaff"        
 [5] "hotmail"          "sdffsddapp"       "mwerqwie"         "niweqrerw"       
 [9] "hotmail"          "moweqrewsfaslay" 

or if you want everything after the @:

sub(".*@","",emails)

 [1] "hotmail.com"          "iueywapp.com"         "lasdfsdfsdstores.com"
 [4] "mobsdaff.com"         "hotmail.org"          "sdffsddapp.com"      
 [7] "mwerqwie.com"         "niweqrerw.tv"         "hotmail.com"         
[10] "moweqrewsfaslay.com" 
Daniel O
  • 4,258
  • 6
  • 20
2

You can try the following:

str_sub(emails, str_locate(emails, "@")[,1]+1) 

Output:

[1] "hotmail.com"  "iueywapp.com"  "lasdfsdfsdstores.com" "mobsdaff.com"        
[5] "hotmail.org"  "sdffsddapp.com"  "mwerqwie.com"      "niweqrerw.tv"        
[9] "hotmail.com"  "moweqrewsfaslay.com" 
Eric
  • 2,699
  • 5
  • 17
  • I was able to figure out how to remove everything the left of @. Does anyone know how I add so we can also remove everything to the right of "." as well? – Eric May 21 '20 at 17:18
1

We can use trimws from base R

trimws(emails, whitespace= '.*@')
#[1] "hotmail.com"          "iueywapp.com"         "lasdfsdfsdstores.com" "mobsdaff.com"         "hotmail.org"          "sdffsddapp.com"      
#[7] "mwerqwie.com"         "niweqrerw.tv"         "hotmail.com"          "moweqrewsfaslay.com" 
trimws(emails, whitespace= '.*@|\\..*')
#[1] "hotmail"          "iueywapp"         "lasdfsdfsdstores" "mobsdaff"         "hotmail"          "sdffsddapp"       "mwerqwie"        
#[8] "niweqrerw"        "hotmail"          "moweqrewsfaslay" 
akrun
  • 874,273
  • 37
  • 540
  • 662