(Update: I see responses assuming this is a balanced delimiter question; it is not, it's more a parsing perl question. The brackets in a regex don't necessarily balance.)

Taking just the [] part into consideration, what you want to match is going to be zero or more (one or more?): of either a series of other characters or a [...] pair. So at first blush, something like this:

m/\[ # initial [ (?: [^][]+ # non-bracket parts | \[somethinginbrackets\] # something in brackets )* # repeated \] # final ] /x
But (as that regex itself shows) the something in brackets itself could be a little complicated to parse.

First, note that if you allow interpolated variables in your regex, the problem is a lot more complicated. $foo[bar] in a regex could be either the scalar $foo followed by a character class, or an interpolated array element. Perl uses a guessing function that gives different weights to characters found in the "bar" part to decide which (and doesn't always get it "right"). If "bar" is an array index, it will be arbitrary perl code. See "Gory details of parsing quoted constructs" in perlop for how to locate the ending ]. Also, you need to allow for interpolated aribitrary code: @{[foo(),"]"]}. That's not a matter of matching square brackets; the gory details rules will help find the ending "}".

Assuming you don't need to handle interpolation in the regex, you still have to deal with []], [x[:alnum:]y] (vs. [x[:alnum]), [[\]], and similar. See "Version 8 Regular Expressions" and the POSIX character class sections in perlre. (Update: previous sentence unmangled.)

Update: all things considered, you'd do yourself a great favor requiring your value to be quoted with "" or something else (with literal " in the value requiring a backslash) when the value has a ]. Or just require all brackets in the value to be backslashed. Then it's just a matter of matching m/\[ (?: [^][]+  |  \\. )* \]/x.

Update: ++halley; I'm going to look at Text::Balanced.


In reply to Re: Regex dynamics by ysth
in thread Regex dynamics by Hena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.