Let's say this is my string
string<- c("righttoleftrightandleft")
I want to extract all the character between right and left so the result will be something like that
results<-("to","and")
string<- c("righttoleftrightandleft")
vec <- strsplit(string, split = "left")
result <- c( sub(pattern = "right", "", vec[[1]][1:2]) )
Please try this with s <- "righttoleftrightandleft"
strsplit(gsub("right(.*?)left", "\\1 ", s), split="\\s")[[1]]
This gives a vector:
[1] "to" "and"
Note that:
gsub
extracts all elements between parentheses, each stored in \\\\1
?
is required for non-greedy match strsplit
splits the resulting match on whitespace Could try:
gsub("right(.*?)left", "\\1", regmatches(string, gregexpr("right(.*?)left",string))[[1]])
Where regmatches(...)
returns matches sequences and gsub(...)
extracts the word in the middle.
You can also use the following:
text="righttoleftrightandleft"
A=unlist(strsplit(text,"right|left",))
A[A!=""]
[1] "to" "and"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.