number of regex match

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 8
Reputation: perl1user is an unknown quantity at this point 
Solved Threads: 0
perl1user perl1user is offline Offline
Newbie Poster

number of regex match

 
0
  #1
Jan 15th, 2009
hi,
i'd like to know how to get the number of regexp matches a pattern returns
ex: if I have $_ = "fooFOObarBAR";$_ =~ /[a-z]/
it would return 6

thx

SNIP
Last edited by happygeek; Jan 15th, 2009 at 7:31 pm. Reason: email snipped
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: number of regex match

 
0
  #2
Feb 5th, 2009
  1. $count=($value)=regex;
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: number of regex match

 
0
  #3
Feb 5th, 2009
But you have to remove the second $_, that is resetting the value.

  1. $_ = "fooFOObarBAR";
  2. $count=(@var)=/[a-z]/g;
  3. print "$count @var\n";
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: number of regex match

 
0
  #4
Feb 5th, 2009
Try not to back to back post... it's bad etiquette (though sometimes necessary). The better alternative is to edit your post within the grace-period provided by the site.
Last edited by Comatose; Feb 5th, 2009 at 11:03 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: number of regex match

 
0
  #5
Feb 5th, 2009
OK, will do. Sorry. I am new to these forums, but not new to perl. I will edit in the future. My bad.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: number of regex match

 
0
  #6
Feb 5th, 2009
An interesting construct to do the same thing. No need to actually use a named array:

  1. $_ = "fooFOObarBAR";
  2. $count=()=/[a-z]/g;
  3. print "$count\n";

But I would do it like this myself:

  1. $_ = "fooFOObarBAR";
  2. $count = tr/a-z/a-z/;
  3. print "$count\n";
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: number of regex match

 
0
  #7
Feb 5th, 2009
Well, I would normally have left the named array off and just done it with a simple scalar, but since the questioner is a newbie to perl, I wanted to show where the values end up when you use () and when you use just a scalar. I read a funny article in perl journal years ago (it was satire) in which the guy was supposedly using perl for military technology and forgot the () in the tolerances, so he was getting a "1" because it was the count of matches, rather than the value of matches.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: number of regex match

 
0
  #8
Feb 5th, 2009
Cool. Its sort of moot anyway in this example since tr/// is really the better way to do it since it involves no pattern matching, just simple character recognition.

I just thought the construct was interesting:

$count = () = /[a-z]/g;

I remember the first time I saw that years ago and it was a bit of a revelation in how perl works.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: number of regex match

 
0
  #9
Feb 5th, 2009
Yeah, I find that interesting as well. Perl is amazing - thanks Larry!

I told a co-worker that I use perl everyday and he said "I hate that language" so I immediately lost all respect for him (haha).

You're right tr/// is better and more efficient. I sometimes use some old tricks because I have never had anyone actually teach me perl, just figured out by doing. I am now writing (most) everything object oriented in perl - which I hadn't before because I wrote most stuff very C-like.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: number of regex match

 
0
  #10
Feb 5th, 2009
Well, we are in the same boat, I never learned perl formally either. Last time i was in school I was learning how to write programs on punch cards. Which totally turned me off to programming for a very long time.
Last edited by KevinADC; Feb 5th, 2009 at 2:00 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC