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

后端 未结 4 1334
慢半拍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"
    
    0 讨论(0)
  • 2021-01-21 14:39
    sub('(.*),.*','\\1', a, perl=T)
    [1] "Experiment A"
    
    0 讨论(0)
  • 2021-01-21 14:42

    Lookahead is made for this:

    b <- regexpr(".*(?=, useless)", a, perl=TRUE)
    regmatches(a, b)
    ## [1] "Experiment A"
    

    .* matches any sequence of characters, but the lookahead (?=, useless) says that it only matches text that is followed by the string ", useless".

    0 讨论(0)
  • 2021-01-21 14:46

    sub("(\\w*), useless.*","\\1",a)

    0 讨论(0)
提交回复
热议问题