如何在 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+ 瀏覽

開啟你的 職業生涯

透過完成該課程獲得認證

開始
廣告
© . All rights reserved.