[R] Split String in regex while Keeping Delimiter

Emily Bakker em||yb@kker @end|ng |rom out|ook@com
Wed Apr 12 10:29:50 CEST 2023


Hello List,
 
I have a dataset consisting of strings that I want to split while saving the delimiter.
 
Some example data:
“leucocyten + gramnegatieve staven +++ grampositieve staven ++”
“leucocyten – grampositieve coccen +”
 
I want to split the strings such that I get the following result:
c(“leucocyten +”,  “gramnegatieve staven +++”,  “grampositieve staven ++”)
c(“leucocyten –“, “grampositieve coccen +”)
 
I have tried strsplit with a regular expression with a positive lookahead, but I am not able to achieve the results that I want.
 
I have tried:
as.list(strsplit(x, split = “(?=[\\+-]{1,3}\\s)+, perl=TRUE)
 
Which results in:
c(“leucocyten “, “+”,  “gramnegatieve staven “, “+”, “+”, “+”,  “grampositieve staven ++”)
c(“leucocyten “, “–“, “grampositieve coccen +”)
 
 
Is there a function or regular expression that will make this possible?
 
Kind regards,
Emily 
 


More information about the R-help mailing list