How to extract the strings between two words in R?


While dealing with text data, we sometimes need to extract values between two words. These words can be close to each other, at the end sides or on random sides. If we want to extract the strings between two words then str_extract_all function of stringr package can be used.

Loading stringr package −

library(stringr)

Example1

 Live Demo

x1<−"Tutorialspoint is the best resource for tutorials and courses"
x1
[1] "Tutorialspoint is the best resource for tutorials and courses"
str_extract_all(x1,"(?<=Tutorialspoint).+(?=courses)")
[[1]]
[1] " is the best resource for tutorials and "

Example2

 Live Demo

x2<−"Life is what happens when you're busy making other plans"
x2
[1] "Life is what happens when you're busy making other plans"
str_extract_all(x2,"(?<=Life).+(?=plans)")
[[1]]
[1] " is what happens when you're busy making other "

Example3

 Live Demo

x3<−"I’m selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can’t handle me at my worst, then you sure as hell don’t deserve me at my best."
x3
[1] "I’m selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can’t handle me at my worst, then you sure as hell don’t deserve me at my best."
str_extract_all(x3,"(?<=selfish).+(?=But)")
[[1]]
[1] ", impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. "

Example4

 Live Demo

x4<−"Get busy living or get busy dying."
x4
[1] "Get busy living or get busy dying."
str_extract_all(x4,"(?<=Get).+(?=dying)")
[[1]]
[1] " busy living or get busy "

Example5

x5<−"The first step toward success is taken when you refuse to be a captive of the environment in which you first find yourself"
x5
[1] "The first step toward success is taken when you refuse to be a captive of the environment in which you first find yourself"
str_extract_all(x5,"(?<=toward).+(?=yourself)")
[[1]]
[1] " success is taken when you refuse to be a captive of the environment in which you first find "

Example6

x6<−"When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one which has been opened for us"
x6
[1] "When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one which has been opened for us"
str_extract_all(x6,"(?<=but).+(?=us)")
[[1]]
[1] " often we look so long at the closed door that we do not see the one which has been opened for "

Example7

x7<−"Twenty years from now you will be more disappointed by the things that you didn’t do than by the ones you did do."
x7
[1] "Twenty years from now you will be more disappointed by the things that you didn’t do than by the ones you did do."
str_extract_all(x7,"(?<=more).+(?=.)")
[[1]]
[1] " disappointed by the things that you didn’t do than by the ones you did do"

Example8

x8<−"When I dare to be powerful − to use my strength in the service of my vision, then it becomes less and less important whether I am afraid."
x8
[1] "When I dare to be powerful − to use my strength in the service of my vision, then it becomes less and less important whether I am afraid."
str_extract_all(x8,"(?<=use).+(?=and)")
[[1]]
[1] " my strength in the service of my vision, then it becomes less "

Example9

x9<−"Great minds discuss ideas; average minds discuss events; small minds discuss people."
x9
[1] "Great minds discuss ideas; average minds discuss events; small minds discuss people."
str_extract_all(x9,"(?<=;).+(?=small)")
[[1]]
[1] " average minds discuss events; "

Example10

x10<−"A successful man is one who can lay a firm foundation with the bricks others have thrown at him."
x10
[1] "A successful man is one who can lay a firm foundation with the bricks others have thrown at him."
str_extract_all(x10,"(?<= ).+(?=.)")
[[1]]
[1] "successful man is one who can lay a firm foundation with the bricks others have thrown at him"

Updated on: 06-Nov-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements