REGEX in R: extracting words from a string

后端 未结 2 643
走了就别回头了
走了就别回头了 2021-01-05 15:38

i guess this is a common problem, and i found quite a lot of webpages, including some from SO, but i failed to understand how to implement it.

I am new to REGEX, and

2条回答
  •  逝去的感伤
    2021-01-05 16:27

    For getting the first four words.

    library(stringr)
    str_extract(x, "^\\s*(?:\\S+\\s+){3}\\S+")
    

    For getting the last four.

    str_extract(x, "(?:\\S+\\s+){3}\\S+(?=\\s*$)")
    

提交回复
热议问题