0

Trying to split and include. So far, I can do the look behind ok.

string_txt <- "apple eat bear"

strsplit(string_txt, "(?<=eat)", perl = TRUE)
[[1]]
[1] "apple eat" " bear"

However, when I try a look ahead, the word 'eat' is broken apart.

strsplit(string_txt, "(?=eat)", perl = TRUE)
[[1]]
[1] "apple "  "e"       "at bear"
user2340706
  • 361
  • 2
  • 12
  • 1
    Hmm what's the split rule of `"(?=eat)"`? I'm no regex master, however `"\\s(?=eat)"` or sth would make sense to me, i.e. split on space followed by eat. – lukeA Jan 27 '16 at 19:40
  • 1
    The linked duplicate explains why this happens. Note that you can get the desired behavior, however, with `str_split` in the stringr package; e.g. `stringr::str_split("apple eat bear", "(?=eat)")` – David Robinson Jan 27 '16 at 19:42
  • @DavidRobinson is right, this is an unexpected "feature" of `strsplit`. I second the recommendation of using `stringr`'s `str_split`. – Carlos Cinelli Jan 27 '16 at 19:49

0 Answers0