简体   繁体   中英

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

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM