How to extract a string that lies between two strings in R?


If we have a long string then we might want to extract a part of string that lies between two strings. For example, if we have a string “E-learning changing the education system in the world” and we want to extract the string “the education system” brave then we must be very careful about passing the strings in string function, you get to know this in examples. The extraction is not difficult with gsub function but we have to make sure that we are using the correct syntax, otherwise, the result will become obnoxious.

Examples

x1<-"E-learning changing the education system in the world"
gsub(".*changing (.+) in.*", "\1",x1)
[1] "the education system"
gsub(".*the (.+) system.*", "\1",x1)
[1] "education"
x2<-"Tutorialspoint helping programmers around the world"
gsub(".*helping (.+) around.*", "\1",x2)
[1] "programmers"
gsub(".*Tutorialspoint (.+) programmers.*", "\1",x2)
[1] "helping"
gsub(".*helping (.+) the.*", "\1",x2)
[1] "programmers around"
x3<-"kindness is a mark of faith, and whoever has not kindness has not faith"
gsub(".*a (.+) of.*", "\1",x3)
[1] "mark"
gsub(".*and (.+) has.*", "\1",x3)
[1] "whoever has not kindness"
gsub(".*has (.+) has.*", "\1",x3)
[1] "not kindness"
gsub(".*has (.+) faith.*", "\1",x3)
[1] "not"
gsub(".*and (.+) faith.*", "\1",x3)
[1] "whoever has not kindness has not"
gsub(".*of (.+) whoever.*", "\1",x3)
[1] "faith, and"
gsub(".*of (.+) and.*", "\1",x3)
[1] "faith,"
x4<-"None of you truly believes until he wishes for his brother what he wishes for himself."
gsub(".*of (.+) truly.*", "\1",x4)
[1] "you"
gsub(".*believes (.+) until.*", "\1",x4)
[1] "None of you truly believes until he wishes for his brother what he wishes for himself."
gsub(".*believes (.+) for.*", "\1",x4)
[1] "until he wishes for his brother what he wishes"
gsub(".*you (.+) until.*", "\1",x4)
[1] "truly believes"
gsub(".*his (.+) what.*", "\1",x4)
[1] "brother"
gsub(".*he (.+) for.*", "\1",x4)
[1] "wishes"
gsub(".*until (.+) for.*", "\1",x4)
[1] "he wishes for his brother what he wishes"
gsub(".*truly (.+) until.*", "\1",x4)
[1] "believes"
gsub(".*truly (.+) what.*", "\1",x4)
[1] "believes until he wishes for his brother"
gsub(".*truly (.+) for.*", "\1",x4)
[1] "believes until he wishes for his brother what he wishes"
gsub(".*until (.+) what.*", "\1",x4)
[1] "he wishes for his brother"
x5<-"To overcome evil with good is good, to resist evil by evil is evil."
gsub(".*To (.+) with.*", "\1",x5)
[1] "overcome evil"
gsub(".*good (.+) good.*", "\1",x5)
[1] "is"
gsub(".*resist (.+) evil.*", "\1",x5)
[1] "evil by evil is"
gsub(".*to (.+) evil.*", "\1",x5)
[1] "resist evil by evil is"
gsub(".*evil (.+) evil.*", "\1",x5)
[1] "is"
gsub(".*To (.+) evil.*", "\1",x5)
[1] "overcome evil with good is good, to resist evil by evil is"
gsub(".*good, (.+) resist.*", "\1",x5)
[1] "to"
gsub(".with (.+) is.*", "\1",x5)
[1] "To overcome evilgood is good, to resist evil by evil"

Updated on: 24-Aug-2020

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements