【发布时间】:2016-11-06 23:14:52
【问题描述】:
我的理解是 R 使用扩展正则表达式或类似 Perl 的正则表达式。我已经在 SO 和网络上搜索了这个正则表达式问题的解决方案,但我发现是空的:
在 R 中,我有一个文本文件向量。每个元素由几个段落组成。我想从每个元素中提取几句话来用这个文本子集创建一个新向量。我要提取的句子遵循可预测的模式。
text <- c("AND \n \n house notes: text text/text.\n \n text text \n text",
"AND \n \n notes: text text/text.\n \n text text \n text",
"AND \n \n house: text text/text.\n \n text text \n text")
我想提取“house notes”、“house”或“notes”与第一个“\n”之间的所有文本。 “house notes”、“house”或“notes”这些词可能在文档中的其他位置,但我对它们的第一次出现感兴趣。
> output
"house notes: text text/text.\n",
"notes: text text/text.\n ",
"house: text text/text.\n "
我可以让它在 php \w++ notes: \w++ \w*+[^_]\w[^:\\]*+\\\w 但不是 R.
【问题讨论】:
-
gsub('\n ([^\n]+:[^\n]+)\n|.', '\\1', text)