使用 R 在文本中使用相同模式提取字符串 [英] extract string using same pattern in a text using R

查看:18
本文介绍了使用 R 在文本中使用相同模式提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 R 处理文本,这是我的问题.

I'm trying to deal with text with R and here is my question.

来自这个源文本

#Pray4Manchester# I hope that #ArianaGrande# will be better soon.

我想使用模式 #.+# 提取 Pray4ManchesterArianaGrande,但是当我运行时

I want to extract Pray4Manchester and ArianaGrande using the pattern #.+#, but when I run

str_extract_all(text,pattern="#.+#")

我明白

#Pray4Manchester# I hope that #ArianaGrande#

如何解决这个问题?谢谢.

How to solve this? Thanks.

推荐答案

我们可以做到

str_extract_all(text, "(?<=#)\\w*(?=#)")[[1]]
#[1] "Pray4Manchester" "ArianaGrande"   

数据

text <- "#Pray4Manchester# I hope that #ArianaGrande# will be better soon."

这篇关于使用 R 在文本中使用相同模式提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆