I'd like to extract segments of line from a text. For example:
txt<-"This is some cool text that involves this type of text and not that kind."
extract.context(txt,start="of text",end="that")
"of text and not that"
I'd like to extract segments of line from a text. For example:
txt<-"This is some cool text that involves this type of text and not that kind."
extract.context(txt,start="of text",end="that")
"of text and not that"
It kind of depends on what exactly what you will be looking for. If you will be just searching for characters (no punctuation), then this will work nicely.
extract.context<-function(txt, start, end) {
sapply(regmatches(txt, gregexpr(paste0(start,".*",end),txt)), "[", 1)
}
txt<-"This is some cool text that involves this type of text and not that kind."
extract.context(txt,start="of text",end="that")
# [1] "of text and not that"
This method uses a basic regular expression so if you search for character that may be matched by regular expression syntax, it could get confused. Also it's unclear what you want to do should multiple matches occur. Right now i just return the first. But since you didn't provide a lot of context, i'm going to assume that's OK.