in reply to Match, Capture and get position of multiple patterns in the same string

I highly recommend reading perlvar about all those regex match variables, it's full of "hidden" gems.
#!/usr/bin/perl use strict; use warnings; use 5.010; my $string = "CATINTHEHATWITHABAT"; my $regex = qr{\wAT}i; my @matches = (); while ($string =~ /($regex)/pg){ my $start = $-[0]; my $end = $+[0]; my $hitpos = "$start-$end"; say "${^MATCH} found at $hitpos"; }
Perl 6 - links to (nearly) everything that is Perl 6.
  • Comment on Re: Match, Capture and get position of multiple patterns in the same string
  • Download Code