如何在 R 中提取两个单词之间的字符串?


在处理文本数据时,有时候我们需要提取两个单词之间的值。这些单词可以彼此靠近、在边缘,或在随机的一边。如果我们想提取两个单词之间的字符串,那么可以使用 stringr 包的 str_extract_all 函数。

加载 stringr 包 −

library(stringr)

示例 1

 现场演示

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 "

示例 2

 现场演示

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 "

示例 3

 现场演示

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. "

示例 4

 现场演示

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 "

示例 5

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 "

示例 6

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 "

示例 7

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"

示例 8

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 "

示例 9

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; "

示例 10

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"

更新时间: 06-11-2020

3K+ 浏览

开启你的 职业生涯

通过完成该课程获得认证

开始
广告