【问题标题】:How do I extract a word in front of another certain word?如何在另一个特定单词前面提取一个单词?
【发布时间】:2020-06-18 04:19:53
【问题描述】:

我有一个遵循以下格式的文本字段。

Blessed To Receive An Offer From West Virginia University ... #HailWV üíõüíô
----------
Blessed To Receive An Offer From Michigan State University ... #GoGreen üíö
----------
Blessed to receive an offer from Abilene Christian University!!! #GoWildcats
----------
Im very blessed to announce that I’ve receive an offer from Pacific University! Big thanks to @CoachPatterson_  for this opportunity!
---------
After A Great Talk With @CoachRT_Cisco I’m blessed to receive an offer from Cisco College

我正在尝试提取学院的名称。

我试过下面的代码


str_extract(df$text,'(?<=University)\\[A-Z]\\w')

但它返回的只是 NA 字段?

【问题讨论】:

  • 在任何情况下都可以捕获单词 university 之前的 1 个单词吗?例如,如果您有 "Michigan State University" 怎么办
  • @DanielO 这也是另一个障碍。
  • @user13463162 你能显示所有的模式吗?我的解决方案基于您提供的示例
  • @akrun 刚刚编辑它以显示不同的模式
  • 如果是乔治亚大学,你要提取什么

标签: r tidyverse


【解决方案1】:

这对你有用吗:

数据:

data <- c("Blessed to receive an offer From Texas State University.", 
          "Blessed to receive an offer from Columbia University.",
          "Blessed to receive an offer from Abilene Christian University!!!",
          "Im very blessed to announce that I’ve receive an offer from University of Georgia! Big thanks to @CoachPatterson_  for this opportunity!")

解决方案:

str_extract(data, "(?<=(f|F)rom\\s)[A-Z]\\w+\\b\\s(of\\s)?[A-Z]\\w+\\b(\\s[A-Z]\\w+\\b)?")

结果:

[1] "Texas State University"       "Columbia University"          "Abilene Christian University"
[4] "University of Georgia"

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 2021-05-08
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多