Matching brackets in a string

后端 未结 9 1642
小蘑菇
小蘑菇 2020-12-03 03:47

What is the most efficient or elegant method for matching brackets in a string such as:

\"f @ g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]] // z\"
<
9条回答
  •  长情又很酷
    2020-12-03 04:23

    Edited (there was an error there)

    Is this too naïve?

    doubleB[x_String] :=
      StringReplace[
       ToString@StandardForm@
         ToExpression["Hold[" <> x <> "]"], 
      {"Hold[" -> "", RegularExpression["\]\)$"] -> "\)"}];
    
    doubleB["f[g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]]]"]
    ToExpression@doubleB["f[g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]]]"]
    

    ->

    enter image description here

    Just trying to exploit Mma's own parser ...

提交回复
热议问题