Remove all before the first occurrence of a word gsub regex

Asked

Viewed 209 times

4

This is so simple but I can’t find the answer! I would like to delete everything before the first occurrence of the word "that" in a string:

What am I doing:

v <- c("blabla que 1", "blabla que eu Boqueirão que")
gsub(".*que", "", v)
# [1] " 1"   "irão"

The result I wanted was "1" and "I Boqueirão que" . when I try . * that the command has no effect. Thanks for your help!

  • If you give a space after que will work: gsub(".*que ", "", v). But there must be a more elegant solution than this.

  • this was an example, but the reality my bank is composed of several sentences where the "that" appears several times, so this solution does not work for me.

1 answer

3


I asked in stackoverflow in English and got the answer:

To keep the "what" sub(".*?(que)", "\\1", x) and not to keep the "what" sub(".*?que", "", x)

  • 1

    For explanatory purposes only: The ? after the * in regex makes the pattern non-greedy. This means that he will be satisfied with the first date he has and so solves the problem of the example of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.