-2

I have string in this form

"[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)"

I tried the solutions in the link, but it is not working for this case. I need

"[1425]", "[12545]", "2423", "(125)", "[1543]", "(1432)" 

ie. remove the multiples of bracket present.

Community
  • 1
  • 1

2 Answers2

1

Try:

library(stringr)
v1 <- c("[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)")

str_extract(v1, "\\D?[0-9]+\\D?")
#[1] "[1425]"  "[12545]" "2423"    "(125)"   "[1543]"  "(1432)" 
akrun
  • 874,273
  • 37
  • 540
  • 662
0

And the award for most unreadable answer goes to:

x <- c("[1425]", "[[12545]]", "2423", "((125))", "[[[1543]]]", "(1432)")
gsub("(\\[|\\]|\\(|\\))\\1+","\\1",x)
#[1] "[1425]"  "[12545]" "2423"    "(125)"   "[1543]"  "(1432)"
thelatemail
  • 91,185
  • 12
  • 128
  • 188