Exclude everything after the second occurrence of a certain string

后端 未结 2 1763
清酒与你
清酒与你 2021-01-18 07:39

I have the following string

string <- c(\'a - b - c - d\',
            \'z - c - b\',
            \'y\',
            \'u - z\')

I would

2条回答
  •  醉梦人生
    2021-01-18 08:00

    Note that you cannot use a negated character class to negate a sequence of characters. [^ - ]*$ matches any 0+ chars other than a space (yes, it matches -, too, because the - created a range between a space and a space) followed by the end of the string marker ($).

    You may use a sub function with the following regex:

    ^(.*? - .*?) - .*
    

    to replace with \1. See the regex demo.

    R code:

    > string <- c('a - b - c - d', 'z - c - b', 'y', 'u - z')
    > sub("^(.*? - .*?) - .*", "\\1", string)
    [1] "a - b" "z - c" "y"     "u - z"
    

    Details:

    • ^ - start of a string
    • (.*? - .*?) - Group 1 (referred to with the \1 backreference in the replacement pattern) capturing any 0+ chars lazily up to the first space, hyphen, space and then again any 0+ chars up to the next leftmost occurrence of space, hyphen, space
    • - - a space, hyphen and a space
    • .* - any zero or more chars up to the end of the string.

提交回复
热议问题