-2

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"
tshepang
  • 12,111
  • 21
  • 91
  • 136
asosnovsky
  • 2,158
  • 3
  • 24
  • 41
  • 3
    That is a very brief description with no sample input or expected output. Perhaps you could create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it clear what you currently have working and what you need help with. – MrFlick Jun 16 '14 at 20:22

1 Answers1

1

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295