R regex find last occurrence of delimiter

后端 未结 4 1198
情深已故
情深已故 2020-12-06 13:58

I\'m trying to get the ending for email addresses (ie .net, .com, .edu, etc.) but the portion after the @ can have multiple periods.

library(stringi)

stri         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-06 14:43

    A read.table + file_ext approach (not regex but pretty easy):

    dat <- read.table(text=strings1, sep="@")
    dat$V3 <- tools::file_ext(strings1)
    dat
    
    ##     V1            V2  V3
    ## 1 test       aol.com com
    ## 2 test   hotmail.com com
    ## 3 test    xyz.rr.edu edu
    ## 4 test abc.xx.zz.net net
    

    Here's a purely regex approach:

    do.call(rbind, strsplit(strings1, "@|\\.(?=[^\\.]+$)", perl=TRUE))
    
    ##     [,1]   [,2]        [,3] 
    ## [1,] "test" "aol"       "com"
    ## [2,] "test" "hotmail"   "com"
    ## [3,] "test" "xyz.rr"    "edu"
    ## [4,] "test" "abc.xx.zz" "net"
    

提交回复
热议问题