learningperl01 has asked for the wisdom of the Perl Monks concerning the following question:
Hello I am hoping someone can point me in the right direction. I have the following code shown below. What I am trying to do is print the count/total number of matches seen in the regex along with the name of the match that were found.
Currently the script prints the number of matches per regex string instead of the total number. I would like to see the results as shown under "wanted results" shown below. Thanks for the help in advance!!
Currently the script prints the number of matches per regex string instead of the total number. I would like to see the results as shown under "wanted results" shown below. Thanks for the help in advance!!
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { my $string_found = $_; my @match = ( $string_found =~ /(first_string|second_string|third_stri +ng|fourth_string|fifth_string)/gm ); print scalar @match," matches found: ",join ", ",@match, "\n"; } __DATA__ This is a test file matchme ljldjlfjd l;djfldjlf d test test test dljfldjlfjldjfldjlljdf one second_string dlfjldfj ljdfldjjf ldjfljdl dfljdlfj dfdlfj three ljfldjlj dlfjlasdj foiidufoiida matchdf dljfldsaofuoidfousdaof ladsjflasdof first_string dlfjodsuofuasdo sadoufosadu foasduf aosduf third_string __Current results__ 0 matches found: 1 matches found: second_string, 0 matches found: 0 matches found: 1 matches found: first_string, 1 matches found: third_string, __Wanted results__ 3 matches found: second_string, first_string, third_string
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Printing the count for regex matches
by bellaire (Hermit) on Mar 10, 2009 at 01:26 UTC | |
Re: Printing the count for regex matches
by Lawliet (Curate) on Mar 10, 2009 at 01:28 UTC | |
Re: Printing the count for regex matches
by jethro (Monsignor) on Mar 10, 2009 at 01:32 UTC | |
Re: Printing the count for regex matches
by hbm (Hermit) on Mar 10, 2009 at 01:41 UTC | |
by CountZero (Bishop) on Mar 10, 2009 at 05:49 UTC | |
Re: Printing the count for regex matches
by Marshall (Canon) on Mar 10, 2009 at 09:20 UTC | |
Re: Printing the count for regex matches
by johngg (Canon) on Mar 10, 2009 at 09:33 UTC |
Back to
Seekers of Perl Wisdom