Utilizing Perl 5.6 regex features, you can do the following:

our $parens; my $re = qr{ ( \( (?{$parens++ if $parens >= 0}) | \) (?{$parens-- if $parens >= 0}) | . )* }x; my $this = 'a(bc(de)fg)h'; my $that = '(a(bc(de)fg)h'; my $other = 'a(bc(de)fg)h)'; my $bla = ')a(bc(de)fg)h)'; $parens = 0; print "this, $parens\n" if $this =~ /$re/; $parens = 0; print "that, $parens\n" if $that =~ /$re/; $parens = 0; print "other, $parens\n" if $other =~ /$re/; $parens = 0; print "bla, $parens\n" if $bla =~ /$re/;

Which prints:

this, 0 that, 1 other, -1 bla, -1

Specifically, -1 indicates that, at some point, an attempt was made to close parens that hadn't been opened. A positive number indicates how many unclosed left parens were encountered.

Thanks, Ilya.

*Woof*


In reply to Re: getting a return code from a looping regular expression by splinky
in thread getting a return code from a looping regular expression by princepawn

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.