regular expressions - counting matches

Posted on 2002-05-16
Last Modified: 2010-03-05
I have an expression along the lines of:


is there simple way to get a list of all the [A|B]\d. that matched and how many times each one was encountered?

Question by:_D_
  • 2
LVL 84

Expert Comment

ID: 7015215
@list = /KEYWORD([A|B]\d.+?)/g
LVL 84

Expert Comment

ID: 7015502
$howmanytimes = s/KEYWORD([A|B]\d.+?)/\1/g;

Accepted Solution

andreif earned 50 total points
ID: 7015689
Try this:

map {$hash{$_}++} ($String =~ /KEYWORD([A|B]\d.+?)/g);

# as a result you get hash with matches as keys and number
# as value. That number shows how many times this match
# encountered.

# I'll print this hash for example

foreach (keys %hash) {
     print "$_: $hash{$_}\n";

Author Comment

ID: 7017061
thanks, just what I was looking for

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse xml file 2 104
Perl Script - Remove row of data based on column value 3 74
Perl string replace for refred url 9 59
iSeries PERL Scripts 7 147
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now