in reply to Re: How can I access the number of repititions in a regex?
in thread How can I access the number of repititions in a regex?

Hi, olus -

Thanks for your quick answers on both accounts.

Ad 1) I understand the solution you propose. It is certainly an option to include all matches into a list and return the size of the list. The downside of this approach is that in more complex regular expressions I need separate lists and regexes for every quantifier I want to access.
while ( <> ) { print "$string_1 matched ", scalar @list_1, "times." if @list_1 = $_~ +/regex_1/g; print "$string_2 matched ", scalar @list_2, "times." if @list_2 = $_=~ + /regex_1/g; }


Clearly, this could get messy ... are there any alternatives to this?

Ad 2) Good stuff ... the code tags now work! How do I indent?

Thanks again!

Cheers -

Pat

Replies are listed 'Best First'.
Re^3: How can I access the number of repititions in a regex?
by Fletch (Bishop) on Mar 11, 2008 at 13:16 UTC
    Erm, just use that big wide key at the bottom of your keyboard? (Burma Shave)

    Update: And to clarify what I think you're saying your problem is: you've got a regex with multiple captures of varying length (say, /(a+)(b+)/) and you want to know how many repetitions each captured subexpression matched (i.e. how many "a"s and how many "b"s) for an arbitrary regex.

    Which is actually a kind of neat question (and I'm drawing a blank of an "elegant" solution off the cuff; I initially was going to comment about the operator-which-shall-not-be-named too (=()=) but then read the above post and saw you had possibly several subexpressions to count).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      On the off-chance that you're asking about indenting OUTSIDE <c>...</c> tags, try <blockquote>...</blockquote> which

      works like this, indenting both sides from the maintext margins and wrapping long lines like this one.

      HTH

      Fletch -

      Your are spot on in your interpretation of the problem.
      But what is your solution?

      And if thou shallt not see, there will not be another but one.

      Pat
Re^3: How can I access the number of repititions in a regex?
by Anonymous Monk on Mar 11, 2008 at 14:14 UTC
    We call it programming
    while(<>){ for my $pat ( qr/\d/, qr/string/ ){ my $count = () = /$pat/g; print "$pat matched $count times\n"; } }
Re^3: How can I access the number of repititions in a regex?
by olus (Curate) on Mar 11, 2008 at 14:42 UTC

    The first solution that occurred to me is shown in the following code. Note that I considered splitting the input text on spaces, and that may not be a solution for you depending on your actual input and the patterns you are looking for.

    use strict; use warnings; use Data::Dumper; my $text = "match other match not useful match sample word"; my $string1 = "match"; my $string2 = "not"; my %repetitions; map {$repetitions{$_}++;} grep /$string1|$string2/, split / /, $text; print Dumper(\%repetitions); ### Outputs: $VAR1 = { 'match' => 3, 'not' => 1 };
Re^3: How can I access the number of repititions in a regex?
by thundergnat (Deacon) on Mar 11, 2008 at 20:12 UTC

    Abstract the matching logic out into a subroutine

    use warnings; use strict; my @strings = qw/ 1 2 3 4 5 6 7 8 90 /; while ( my $line = <DATA> ) { print $line; for (@strings) { my $count = matches( $line, $_ ); print "$_ matched $count times.\n" if $count; } print "\n"; } sub matches { return () = $_[0] =~ /\Q$_[1]\E/g; } __DATA__ 9087126348716340789126348907164 l3klj09934u098u5tio2354uj908rye qoiriopuj3u45098479183248r95r77 [q9u4r0983u490ru340u54ioeuf9p8h 23qioh89174y9843y7r9843r87e8714 [9490838945r8974r9834093409tr34