Extract text between certain symbols using Regular Expression in R

后端 未结 5 1308
刺人心
刺人心 2020-12-05 20:35

I have a series of expressions such as:

\"the text I need to extract
\"

I need to extrac

5条回答
  •  独厮守ぢ
    2020-12-05 21:10

    If there is only one ... as in the example then match everything up to and everything from forward and replace them both with the empty string:

    x <- "the text I need to extract
    " gsub(".*|.*", "", x)

    giving:

    [1] "the text I need to extract"
    

    If there could be multiple occurrences in the same string then try:

    library(gsubfn)
    strapplyc(x, "(.*?)", simplify = c)
    

    giving the same in this example.

提交回复
热议问题