我有这种形式的字符串
"[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)"
我尝试了link中的解决方案,但它不适合这种情况。我需要
"[1425]", "[12545]", "2423", "(125)", "[1543]", "(1432)"
即。删除存在的多个括号。
答案 0 :(得分:1)
尝试:
library(stringr)
v1 <- c("[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)")
str_extract(v1, "\\D?[0-9]+\\D?")
#[1] "[1425]" "[12545]" "2423" "(125)" "[1543]" "(1432)"
答案 1 :(得分:0)
最难以理解的答案是:
x <- c("[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)")
gsub("(\\[|\\]|\\(|\\))\\1+","\\1",x)
#[1] "[1425]" "[12545]" "2423" "(125)" "[1543]" "(1432)"