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


如果我们有一长串字符串,我们可能希望提取两个字符串之间的一部分字符串。例如,如果我们有一串字符串“电子学习改变了世界上的教育体系”,我们希望提取字符串“教育体系”,那么我们必须非常小心地传递 string 函数中的字符串,你会在示例中了解到这一点。使用 gsub 函数提取并不难,但我们必须确保使用正确的语法,否则结果将变得令人讨厌。

示例

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"

更新于:2020 年 8 月 24 日

466 次浏览

启动你的 职业

完成课程获得认证

开始
广告