Skip to main content
2 of 3
Commonmark migration

Perl 6, 53 bytes

The closure:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}

Needs an argument, eg:

{my ($m,$d);/[\[{$d++;$m=max $m,$d}|\]{$d--}|.]*/;$m}("[[[3]][2]]")
3

Explanation:

{ my ($m,$d);                 # start closure, declare variables    

  /                           # start regex match

   [                          # start (non-capturing) group

     \[ {$d++;$m=max $m,$d} | # match [ and track depth; OR

     \] {$d--}              | # match ] and track depth; OR

     .                        # match one character

   ]*                         # repeat group

  /;                          # end regex

  $m                          # return max depth
}
raiph
  • 121
  • 5