Regex in R: matching the string before a sequence of characters

后端 未结 4 1341
慢半拍i
慢半拍i 2021-01-21 14:05

I want to extract a part of the string that comes before a certain word. E.g. I want to get everything before \", useless\".

a <- \"Experiment A, useless (03/         


        
4条回答
  •  执念已碎
    2021-01-21 14:34

    We can use sub to match the , followed by zero or more spaces (\\s*) followed by 'useless' and other characters that follow (.*) and replace it with blank ("")

    sub(",\\s*useless\\b.*", "", a)
    #[1] "Experiment A"
    

提交回复
热议问题