Try:
#!/usr/bin/perl -w
use strict;
$_ = "ABabcde12345BAABabcde1234blahblah5BA";
print "is: ", $_ , "\n";
my $count = () = $_=~ /(AB.*?BA)/g;
my @match = /(AB.*?BA)/g;
print "match: ", join(", ", @match), "\n";
print "I matched $count times\n";
The trick is:
$_ =~ /../
> You can also use this bizarre construction to capture just the count:
>
> $_ = "ABabcde12345BAABabcde1234blahblah5BA";
> $count = () = $_=~ /(AB.*?BA)/g;
> print "I matched $count times\n"'
>
> the () between the two ='s forces the match to list context, and then
> THAT is forced t
You can also use this bizarre construction to capture just the count:
$_ = "ABabcde12345BAABabcde1234blahblah5BA";
$count = () = $_=~ /(AB.*?BA)/g;
print "I matched $count times\n"'
the () between the two ='s forces the match to list context, and then
THAT is forced to scalar context
> How do I get a count of the number of matches a regex finds within a
string?
>
> example:
>
> $_ = "ABabcde12345BAABabcde1234blahblah5BA";
> print $1 if /(AB.*?BA)/ ;
>
> Ultimate goal is to separate with \n.
>
> Terry
Try this:
$string = "ABabcde12345BAABabcde1234blahblah5BA";
$count = $st
=~ m//g returns the number of matches if evaluated in list context so...
$_ = "ABabcde12345BAABabcde1234blahblah5BA";
print $1 if /(AB.*?BA)/ ;
becomes
$_ = "ABabcde12345BAABabcde1234blahblah5BA";
@count=$_=~/(AB.*?BA)/g;
print "I matched " . scalar @count . " times\n";
If y