问题
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