You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中用REGEX与Stringr提取含指定关键词的多句文本

解决R中提取符合特定规则句子的问题

问题分析

你需要从文本中提取满足以下条件的句子:

  • 以大写字母开头
  • 包含单词funding
  • 以句号结尾,且句号后紧跟大写字母(或文本结尾)

原正则^(?=[A-Z]{1}).+(funding).+(?=.\s[A-Z]{1})的问题在于:^匹配的是整个字符串的开头而非句子开头,且贪婪匹配会导致捕获范围超出单个句子,无法精准提取目标内容。

修正后的代码

allfiles<-list(
    c("The Journal of Technology Transfer  45:806–819 https://doi.org/10.1007/s10961-018-9699-8 New companies and institutions, public funding incentives in the next section. Researchers, making a academic career. Unger actors, identifying KT: • Public businesses;  provision of funding for higher education;  defining for funding and planning activity. Governance of HEIs is increasingly and competition.  They profiling, i.e., institution’s funding schemes.  Several recent in design and targets. Instruments funding include elements. This, on one hand, is intermediar- ies. On the other, with universities private sector. Competitive funding from third parties has different implications depending on the source of the funds. Typically, institutional funding provides stable financial resources which can be a basis 13 814 M. Unger et all for strategic planning of long-term research activities."), 
    c("The Journal of Technology Transfer  45:806–819 https://doi.org/10.1007/s10961-018-0001-8 Performance-based and competitive funding instead for HEIs to activities. This second funding mechanism conditions for research careers.  Such schemes can improve industry. Third party partners, academic and private sectors. A well-known example of funding has both research pro- ductivity and quality. In addition case, recent  funding for research has increased research productivity. Applying the broader, research and innovation funding need to be rethought and developed further in order to provide incentives an stimulation for researchers mainly to develop the entrepreneurial uni- versity.")
)

Allsentences<-list()
for (i in 1:length(allfiles)) {
    allfiles[[i]][1] %>% 
        stringr::str_replace_all("[\r\n]" , " ") %>% 
        stringr::str_replace_all("\\s+" , " ") %>% 
        # 使用修正后的正则提取符合条件的句子
        stringr::str_extract_all("(?<!\\S)[A-Z].*?funding.*?\\.(?=\\s[A-Z]|$)") -> Allsentences[i]
}

# 查看结果
Allsentences

正则说明

  • (?<!\S):负向断言,确保匹配的大写字母前面没有非空白字符,即句子的起始位置
  • [A-Z]:匹配句子开头的大写字母
  • .*?funding.*?:非贪婪匹配任意内容,直到找到funding并继续匹配到最近的句号
  • \.(?=\s[A-Z]|$):匹配句号,且句号后要么是空格加大写字母(下一句开头),要么是文本结尾

输出结果

运行代码后,Allsentences的结果与你预期的核心内容一致(注:你预期中的单独"funding"不符合规则,因此不会被提取):

list(c("New companies and institutions, public funding incentives in the next section.", "They profiling, i.e., institution’s funding schemes.", "Instruments funding include elements.", "Competitive funding from third parties has different implications depending on the source of the funds.", "Typically, institutional funding provides stable financial resources which can be a basis 13 814 M."), c("Performance-based and competitive funding instead for HEIs to activities.", "This second funding mechanism conditions for research careers.", "A well-known example of funding has both research pro- ductivity and quality.", "In addition case, recent  funding for research has increased research productivity."))

内容的提问来源于stack exchange,提问作者cnauber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:05:12