http://www.perlmonks.org?node_id=995636


in reply to Re: Limiting number of regex matches
in thread Limiting number of regex matches

Or Maybe this:

use warnings; use strict; my $str = 'dog dog dog dog dog'; print join " " => ( split /\s+/, $str )[ 0 .. 2 ];
Output
dog dog dog

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^3: Limiting number of regex matches
by Anonymous Monk on Sep 25, 2012 at 22:19 UTC
    That doesn't limit the number of matches, it still counts/matches every single dog in the string

      Yes, why use a regex to match for an obvious substring in this case, when a string could be splited into an array, a slice of that which got the required output printed?
      Please, don't forget that the OP also asked among other things "..Is there no other way.."? Am simply showing, some other ways, in this case.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
        And I'm just pointing out the drawbacks