RE: count of matches

2004-12-23 Thread Andy_Bach
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: 
$_ =~ /../
applies the pattern to the string and returns a list of the 2 matches (due 
to the parens inside the RE).  The same thing as:

The empty parens supply the "list context" to the match, and then the 
assignment to $count puts that list into scalar context, akin to:
 my @match = $_ =~ /(AB.*?BA)/g;
my $count = @match;

or:
print "I matched ", scalar @match, " times\n";

Okay, I'm a little shakey on the fact that, the empty parens seem to 
accept the list of return values (anonymous list, akin to an anon. array 
[] ?) and then act like an array (as:
my $count = ('ABabcde12345BA', 'ABabcde1234blahblah5BA');
differs from:
my $count = @match;

in that the former assigns the final element of the list to $count, the 
later assigns the # of elements) but that's the case and thats how the 
matchbox gets opened and counted. 

a

Andy Bach, Sys. Mangler
Internet: [EMAIL PROTECTED] 
VOICE: (608) 261-5738  FAX 264-5932

Call out Gouranga be happy!!!
Gouranga Gouranga Gouranga 
That which brings the highest happiness!!
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: count of matches

2004-12-23 Thread Beckett Richard-qswi266
> 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 by setting it to $count.  Whee!

That can't possibly work. There's no command to open the matchbox, so how can 
you count the matches?

Sorry, last day of work for the year.

R.
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: count of matches

2004-12-22 Thread Kester Allen
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 by setting it to $count.  Whee!
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: count of matches

2004-12-22 Thread Erich Beyrent
> 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 = $string =~ s/(?<=AB)(.*?)(?=BA)/$&/g ;
print "Count = $count\n";

I believe you can also use tr to count matches...

-Erich-

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: count of matches

2004-12-22 Thread Anderson, Mark (Service Delivery)
=~ 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 you don't use the /g modifier it will only match once.

Kind regards,

Mark Anderson
Service Improvement Project
Level 2, 113 Dundas Street
Edinburgh, EH3 5DE
Tel: 0131 523 8786
Mob: 07808 826 063


> -Original Message-
> From: vaughnte [SMTP:[EMAIL PROTECTED]
> Sent: Wednesday, December 22, 2004 11:10 AM
> To:   [EMAIL PROTECTED]
> Subject:  count of matches
> 
> *** WARNING : This message originates from the Internet ***
> 
> 
> Howdy all,
> 
> 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
> 
> ___
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


The Royal Bank of Scotland plc, Registered in Scotland No. 90312. Registered 
Office: 36 St Andrew Square, Edinburgh EH2 2YB

The Royal Bank of Scotland plc is authorised and regulated by the Financial 
Services Authority and represents The Royal Bank of Scotland Marketing Group. 
The Bank sells life policies, collective investment schemes and pension 
products and advises only on the Marketing Group's range of these products and 
on a With-Profit Bond produced by Norwich Union Life (RBS) Limited.

This e-mail message is confidential and for use by the addressee only. If the 
message is received by anyone other than the addressee, please return the 
message to the sender by replying to it and then delete the message from your 
computer. Internet e-mails are not necessarily secure. The Royal Bank of 
Scotland plc does not accept responsibility for changes made to this message 
after it was sent.

Whilst all reasonable care has been taken to avoid the transmission of viruses, 
it is the responsibility of the recipient to ensure that the onward 
transmission, opening or use of this message and any attachments will not 
adversely affect its systems or data. No responsibility is accepted by The 
Royal Bank of Scotland plc in this regard and the recipient should carry out 
such virus and other checks as it considers appropriate.

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs