Dear Monks -

I have been looking into the world of code evaluation expressions of regular expressions recently. Still, I have not been able to come up with a single integrated regex that would match the following set of strings: "Any string of type /a+b+/ that contains exactly one more 'b' than 'a's". The regex I am looking for should therefore match abb, aabbb and aaabbbb but not ab, aab, aabb or abbb.

Clearly, the matching strings could be obtained from the following code:
my @strings = qw( abb aabbb aaabbbb ab aab aabb abbb ); for ( @strings ) { our $a_counter = 0; our $b_counter = 0; if ( /(a(?{$a_counter ++;}))+(b(?{$b_counter ++;}))+/ && $b_counte +r == $a_counter + 1 ) { print "In '$_' there were $a_counter 'a's and $b_counter 'b's. +\n"; } }
As mentioned above, however, I would like to consolidate the information contained in the regex and the if statement into a single regex. It really should match if and only if the number of 'b's exceeds the number of 'a's in the string by one. I don't want any over-generation that I need to filter in a subsequent if statement.

Could anyone please help me out here?

Thanks in advance for your help!

Pat

In reply to Integrating match counts into regex matching by pat_mc

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.