in reply to Re: How can I access the number of repititions in a regex?
in thread How can I access the number of repititions in a regex?

Thanks, GrandFather - I got that.
Nice solution ... though subject to the same limitations as the one by olus above. What if I want to evaluate multiple quantifiers in the same regex?
  • Comment on Re^2: How can I access the number of repititions in a regex?

Replies are listed 'Best First'.
Re^3: How can I access the number of repititions in a regex?
by grizzley (Chaplain) on Mar 11, 2008 at 14:39 UTC

    There is such possibility, but using 'highly experimental' :) code:

    $_='this is some text(55). this is another text. there are 2 texts.'; @re=/text(?{$cnt1++})|\d+(?{$cnt2++})/g; print "'text' occured $cnt1 times\n"; print "'\\d+' occured $cnt2 times\n";

    But there are some problems: what if one of your regexps is part of another one?

      Good stuff, grizley!

      Thanks for throwing code evaluation expressions into the race ... they offer exactly the range of flexibility I need to solve my class of problems ... and - as you can tell by the date on this post - it took me quite some time to realise (a) what they are and (b) how they work.

      Your help is much apprecicated!

      Best regards -

      Pat
Re^3: How can I access the number of repititions in a regex?
by Anonymous Monk on Mar 11, 2008 at 14:09 UTC
    quantifiers are ?, *, +, and {}, so you're not talking about quantifiers
      Oh yes, I am.

      Consider this:
      while ( <> ) { print $a_counter, $b_counter if /a+b+/; }


      with an input like "aaabb" I would expect an output like
      3 2

      Know what I mean?

      Thanks again for taking the time to comment. Your help is much appreciated!
        It sounds like you want something like this:
        for ('aabbbbbc', 'abccc', 'aabcc', 'aaacccc', 'abc') { print length $1, length $2, length $3, "\n" if /(a+)(b+)(c+)/; }
        Though I never would have guessed that's what you were looking for from your initial post. If that doesn't do it, maybe you should post some of the actual data your dealing with and your expected output. -driver8