Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to do a reg expression on a word to find out if it has letters: "a" "b" "c"

I think to get one letter I do this:
m(/a/g);


Now how would i grab "a", "b" , "c"?
m(/abc?/g)


I am basically trying to count how many total I have of "a", "b" "c".

So if I had a word "fileCapaber"
My count would be 4 with the "C" two "a's" and one "b" All I need is the regular expression to fetch those letters out of a word. thanks.

Replies are listed 'Best First'.
Re: Fetching 3 letters only
by thelenm (Vicar) on May 23, 2003 at 19:08 UTC
    Actually, you don't need a regular expression for this... use tr instead.
    my $count = "fileCapaber" =~ tr/AaBbCc//; print "Count is $count\n";

    -- Mike

    --
    just,my${.02}

Re: Fetching 3 letters only
by artist (Parson) on May 23, 2003 at 19:12 UTC
    $_ = q(fileCapaber); my $count = ($_ =~ y/abcABC/abcABC/); print $count;
    artist
      Thanks. Actually I have several words to fetch so I need a reg expression to fetch the specific letters "a" "b" "c"

        If you only want the total

        my $s = 'fileCapaber'; my $count = () = $s =~ m[(a|b|c)]gi; print $count; 4

        but the tr/// solution above is easier and quicker.

        If you want the individual counts, then you could try this.

        print 'Found: ', scalar( () = $s =~ m/($_)/ig), "'${_}'s\n" for qw[a b + c] Found: 2 'a's Found: 1 'b's Found: 1 'c's

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Fetching 3 letters only
by hangmanto (Monk) on May 23, 2003 at 19:22 UTC
    This should fit your requirements. You need to modify the $word variable.

    Remember, when you need to count occurances, the hash is your friend.

    use strict; my $word = 'abcdefabcdeab'; my ( %counters, $total ); while ( $word =~ /([a-c])/gi ) { $counters{$1}++; } foreach my $letter ( sort keys %counters ) { print "\n$letter: " . $counters{$letter}; $total += $counters{$letter}; } print "\nTotal: $total";
    Produces:
    a: 3 b: 3 c: 2 Total: 8
    I hope this helps.
      Wow, Thanks for all the answers. I will print all of them and use for future reference. Thanks again!
Re: Fetching 3 letters only
by BrowserUk (Patriarch) on May 23, 2003 at 19:09 UTC

    Erm... Is this m(/a/g); a Perl regex?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      It most certainly is. It just requires a bit more than just an a to match though.

      Abigail

        Right you are:) I was looking for what I wanted to see, instead of seeing what was there!


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller