【发布时间】:2017-07-10 13:30:28
【问题描述】:
我正在尝试用 R 处理文本,这是我的问题。
来自此源文本
#Pray4Manchester# I hope that #ArianaGrande# will be better soon.
我想使用模式#.+# 提取Pray4Manchester 和ArianaGrande,但是当我运行时
str_extract_all(text,pattern="#.+#")
我明白了
#Pray4Manchester# I hope that #ArianaGrande#
如何解决这个问题?谢谢。
【问题讨论】:
-
它不起作用,因为字符
#也匹配模式.+,这(我猜)导致str_extract贪婪地寻找最广泛的匹配。您将需要本身不包含#的模式,例如 akrun 建议的模式。 -
你需要使用非贪婪修饰符
str_extract_all(text,pattern="#.+?#")