【问题标题】:Search up a line in a paragraph在段落中搜索一行
【发布时间】:2014-06-16 20:11:55
【问题描述】:

我想从文本中提取线段。 例如:

txt<-"This is some cool text that involves this type of text and not that kind."
extract.context(txt,start="of text",end="that")
"of text and not that"

【问题讨论】:

  • 这是一个非常简短的描述,没有样本输入或预期输出。或许您可以创建一个reproducible example 来说明您目前的工作以及您需要帮助的内容。

标签: r text character


【解决方案1】:

这有点取决于您要寻找什么。如果您只是搜索字符(没有标点符号),那么这将很好地工作。

extract.context<-function(txt, start, end) {
    sapply(regmatches(txt, gregexpr(paste0(start,".*",end),txt)), "[", 1)
}


txt<-"This is some cool text that involves this type of text and not that kind."
extract.context(txt,start="of text",end="that")
# [1] "of text and not that"

此方法使用基本的正则表达式,因此如果您搜索可能被正则表达式语法匹配的字符,它可能会混淆。此外,如果发生多个匹配项,您还不清楚您想要做什么。现在我只返回第一个。但由于你没有提供很多上下文,我会假设没关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多