in reply to How to find the outermost pair of square brackets with a regex?

With difficulty.

our $re; local $re = qr{ \[ (?: (?> [^\[\]]+ ) | (??{ $re }) )* \] }x; my $s = <<'__EOI__'; blah blah blah blah blah blah blah blah [blah [blah blah] [blah blah blah blah] blah] blah blah __EOI__ $s =~ s/$re/moo/g; print($s);

Partial credits to perlre.

  • Comment on Re: How to find the outermost pair of square brackets with a regex?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to find the outermost pair of square brackets with a regex?
by lokiloki (Beadle) on Jan 17, 2007 at 01:18 UTC
    that's beautiful... so, if i have this right... match the first bracket, IF, non-backtracking, we find an additional or , then recurse, match the last bracket? what is the purpose of the ^?
      sorry, i meant to say...

      that's beautiful...

      so, if i have this right... match the first bracket; IF, non-backtracking, we find an additional [ or ], then recurse; match the last bracket?

      what is the purpose of the ^?

        oops, caret is inverted character class
      A reply falls below the community's threshold of quality. You may see it by logging in.