in reply to Integrating match counts into regex matching

Hmmm?

our( $a, $b )=(0,0), m[ ( (?<!a) ( a (?{++$a}) )+ ( b (?{++$b}) )+ (?!b) ) (?(?{ $a+1 == $b }) (?=) | (?!) ) ]x and print "'$1'" for qw[ ab abb abbb aabb aabbb aabbbb ];; 'abb' 'aabbb'

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Integrating match counts into regex matching
by ikegami (Patriarch) on Dec 19, 2008 at 08:49 UTC

    You used "(?<!a)" and "(?!b)" as anchors, so your solution was designed to match a substring, but it doesn't work for 'a_abb' (false negative) and 'a_abbb' (false positive).

    for (qw[ ab abb abbb aabb aabbb aabbbb a_abb a_abbb ]) { m[ (?<!a) (?> (a+) (?{ length($^N) }) (b+) ) (?(?{ length($^N)-$^R != 1 })(?!)) ]x and print("$_\n"); }
    abb aabbb a_abb

      Indeed++ Hence the "Hmmm?"

      It can be dealt with:

      for( qw[ ab abb abbb aabb aabbb aabbbb a_abb a_abbb abbaabbbaaabbbaxab +b ] ) { print "$_ contains '$1'" while m[ ( (?<!a) (?{ local $a }) ( a (?{++$a}) )+ ( ?{ local $b }) ( b (?{++$b}) )+ (?!b ) ) (?(?{ $a+1 == $b }) (?=) | (?!) ) ]gx };; abb contains 'abb' aabbb contains 'aabbb' a_abb contains 'abb' abbaabbbaaabbbaxabb contains 'abb' abbaabbbaaabbbaxabb contains 'aabbb' abbaabbbaaabbbaxabb contains 'abb'

      But yours is a simpler approach, and probably quicker.

      But the primary purpose of posting was to demonstrate the regex if...then...else construct--not to do someone else's systems testing for them.

      Particularly as this is very unlikely to be the actual pattern they are trying to match


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Integrating match counts into regex matching
by pat_mc (Pilgrim) on Dec 19, 2008 at 00:04 UTC
    Nice two-liner, BrowserUk!

    I like the coding-elegance.

    Just two (small?) bits I don't understand, the rest is crystal-clear:

    1) What does (?<!a) do?

    2) What does (?!b) do?

    Thanks! - Pat
      1. What does (?<!a) do?

        Zero-length negative lookbehind. Ensures that the string of a's is not preceded by another a.

      2. What does (?!b) do?

        Zero-length negative lookahead. Ensures the matched string of b's is not followed by another b.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Why? I can't think of a situation where omitting the lookahead and the lookbehind will change the answer.

        Nevermind, I didn't realize this was a new thread with a different question.