如何提取R中两个单词之间的字符串?
在处理文本数据时,有时我们需要提取两个单词之间的值。这些词可以在结尾侧或随机侧彼此靠近。如果我们要提取两个单词之间的字符串,则可以使用stringr包的str_extract_all函数。
加载纵梁包-
library(stringr)
例1
x1<−"Nhooo is the best resource for tutorials and courses" x1 [1] "Nhooo is the best resource for tutorials and courses" str_extract_all(x1,"(?<=Nhooo).+(?=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<−"要么忙于生存,要么赶着去死。" x4 [1] "要么忙于生存,要么赶着去死。" 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<−"一个成功的人可以为别人扔给他的砖块打下坚实的基础。" x10 [1] "一个成功的人可以为别人扔给他的砖块打下坚实的基础。" str_extract_all(x10,"(?<= ).+(?=.)") [[1]] [1] "successful man is one who can lay a firm foundation with the bricks others have thrown at him"