Exclude everything after the second occurrence of a certain string

后端 未结 2 1767
清酒与你
清酒与你 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 07:49

    try this (\w(?:\s+-\s+\w)?).*. For the explanation of the regex look this https://regex101.com/r/BbfsNQ/2.

    That regex will retrieve the first tuple if exists or just the first caracter if there's not a tuple. So, the data is get into a "capturing group". Then to display the captured groups, it depends on the used language but in pure regex that will be \1 to get the first group (\2 to get second etc...). Look at the part "Substitution" on the regex101 if you wan't a graphic example.

提交回复
热议问题