How to extract everything until first occurrence of pattern

前端 未结 4 1689
夕颜
夕颜 2021-02-19 00:22

I\'m trying to use the stringr package in R to extract everything from a string up until the first occurrence of an underscore.

What I\'ve tried

4条回答
  •  情歌与酒
    2021-02-19 00:42

    To get L0, you may use

    > library(stringr)
    > str_extract("L0_123_abc", "[^_]+")
    [1] "L0"
    

    The [^_]+ matches 1 or more chars other than _.

    Also, you may split the string with _:

    x <- str_split("L0_123_abc", fixed("_"))
    > x
    [[1]]
    [1] "L0"  "123" "abc"
    

    This way, you will have all the substrings you need.

    The same can be achieved with

    > str_extract_all("L0_123_abc", "[^_]+")
    [[1]]
    [1] "L0"  "123" "abc"
    

提交回复
热议问题