How do I split a line via a delimiter?
Using strsplit
and lookarounds, trimws
afterwards.
strsplit(x, '(?<=\\s)(?=Question)', perl=TRUE) |> lapply(trimws)
# [[1]]
# [1] "Question 1. What is your name?" "Question 2. Where do you live?" "Question 3. How old are you?"
#
# [[2]]
# [1] "Question 1. What are your hobbies?" "Question 2. What is your school?"
# [3] "Question 3. What are your hobbies?"
Data:
x <- c('Question 1. What is your name? Question 2. Where do you live? Question 3. How old are you?',
'Question 1. What are your hobbies? Question 2. What is your school? Question 3. What are your hobbies?')