Split Character String Using Only Last Delimiter in r

后端 未结 4 878
暖寄归人
暖寄归人 2021-01-16 07:38

I have a character variable that I would like to split into 2 variables based on a \"-\" delimiter, however, I would only like to split based on the last delimiter as there

4条回答
  •  一生所求
    2021-01-16 08:20

    You can also use a negative lookahead:

    df <- tibble(input = c("foo - bar", "hey-now-man", "say-now-girl", "fine-now"))
    
    df %>% 
        separate(input, into = c("output1", "output2"), sep = "\\-(?!.*-)", remove = FALSE)
    

    Refs:

    [1] https://frightanic.com/software-development/regex-match-last-occurrence/

    [2] https://www.regular-expressions.info/lookaround.html

提交回复
热议问题