how to remove a subset from a column within square brackets in r

家住魔仙堡 提交于 2019-12-13 18:21:05

问题


I've a column with has multiple strings in square brackets something like this [asdf] [ffgg][ asdf asdf asdf] sdfsgsfbsfg and I've to extract [asdf asdf asdf] it may have only one string like [asdf] or two. Please help


回答1:


We can use str_extract

library(stringr)
str_extract(str1, "\\[(\\w+\\s+){2,}\\w+\\]")    
#[1] "[asdf asdf asdf]"

Or may be

str_extract(str1, "\\[(\\w+\\s+)\\1+[^]]+\\]")
#[1] "[asdf asdf asdf]"

data

str1 <- "[asdf] [ffgg][asdf asdf asdf] sdfsgsfbsfg"


来源:https://stackoverflow.com/questions/44093818/how-to-remove-a-subset-from-a-column-within-square-brackets-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!