How the regex \K can be used in R code?

I tried to use the \K in a separator string definition, but gives an error:

text_to_split = "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet"

tibble(chuck = text_to_split) %>%
  separate_rows(chunk, sep = "amet\K")
#> Error: '\K' is an unrecognized escape in character string starting ""amet\K"

This is what I tried to achieve: regex101: build, test, and debug regex

Previously, I used the lookbehind in my separator string definitions, but the lookbehind is very slow (comparing with the lookahead). Secondly, the lookbehind requires fixed length string definitions (error: 'lookbehind assertion is not fixed length') . Considering these limitations I want to replace the lookbehind in my code. The \K looked like a promising alternative.

(some details on \K: Keep The Text Matched So Far out of The Overall Regex Match)

Try amet\\K for escaping

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.