fsmart fsmart - 1 month ago 10x
R Question

R: strsplit on first instance

I would like to write a strsplit command that grabs the first ")" and splits the string.

For example:

"12" "34)56"

I have read over several other related regex SO questions but I am afraid I am not able to make heads or tails of this. Thank you any assistance.


You could get the same list-type of result as you would with strsplit if you use regexpr to get the first match, and then use the inverted result of regmatches

x <- "12)34)56"
regmatches(x, regexpr(")", x), invert = TRUE)
# [[1]]
# [1] "12"    "34)56"