Exclude everything after the second occurrence of a specific line

I have the following line

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

I would like to multiply it so that everything after the second appearance of “-” is thrown away.

The result is the following:

> string
[1]  "a - b" "z - c" "y"     "u - z"

I used substr(x = string, 1, regexpr(string, pattern = '[^ - ]*$') - 4), but excludes the last occurrence of '-', which I do not want.

+4
source share
2 answers

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

sub :

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

\1. . regex demo.

R-:

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

  • ^ -
  • (.*? - .*?) - 1 ( \1 ), 0+ , , , 0+ , ,
  • - - ,
  • .* - .
+3

(\w(?:\s+-\s+\w)?).*. https://regex101.com/r/BbfsNQ/2.

, , , . ,

0

Source: https://habr.com/ru/post/1671610/


All Articles