How to break a line with dashes outside square brackets

I would like to split the lines as follows:

x <- "abc-1230-xyz-[def-ghu-jkl---]-[adsasa7asda12]-s-[klas-bst-asdas foo]"

by a dash ( -) provided that these strokes should not be contained within the pair []. Expected Result:

c("abc", "1230", "xyz", "[def-ghu-jkl---]", "[adsasa7asda12]", "s",
     "[klas-bst-asdas foo]")

Notes:

  • No nesting square brackets inside each other.
  • Square brackets can contain any characters / numbers / characters except square brackets.
  • The rest of the string is also variable, so we can only assume that we break up -when it's not inside [].

There is a similar question for python ( How to break a string with commas located outside the brackets? ), But I have not yet been able to fine tune this to my script.

+4
3

, ] [:

-(?![^[]*\])

, R:

strsplit(x, "-(?![^[]*\\])", perl=TRUE)

:

  • -:
  • (?! ): : , .
    • [^[]: , [
    • *:
    • \]: ]. , , ], [. , , . : a ] , ( , , [), , ). ( ), , \\].
+9

:

library(stringr)
str_extract_all(x, "(\\[[^\\[]*\\]|[^-])+")
+4

r, , . , , :

  • - [....] char, \x99
  • split by -
  • (/), \x99 -

\[[^]]

+1

Source: https://habr.com/ru/post/1685217/


All Articles