in reply to general regex question

my @names = qw "MacAchallies McAchallies MacAchounich McAchounich MacA +dam McAdam MacAdie McAdie MacAindra"; for(@names){ $_ =~ /M.?c*/ and print $'."\n"; }

Replies are listed 'Best First'.
Re^2: general regex question
by ww (Archbishop) on May 14, 2005 at 16:21 UTC
    fauria:

    Your regex, "/M.?c*/" will do fine at finding "Mac" or "Mc" and printing the REST of the name (rather than what the OP sought), and recall that c* matches ZERO OR MORE "c"s, meaning non-names, like Mccccc (ill-formatted roman numerals??) will match.

    Also, as written, (without capturing parens) it prints:

    
    Achallies
    Achallies
    Achounich
    Achounich
    Adam
    Adam
    Adie
    Adie
    Aindra
    

    whereas (with slightly different use of quantifiers and a couple additional names as test cases), this appears to work as requested:

    #!C:perl/bin my @names = qw "M Mac McA Mcccccc MacAchallies McAchallies MacAchounic +h McAchounich MacAdam McAdam MacAdie McAdie MacAindra"; for(@names){ $_ =~ /(M.?c+.*)/ and print "$1\n"; } =HEAD output is: Mac McA Mcccccc MacAchallies McAchallies MacAchounich McAchounich MacAdam McAdam MacAdie McAdie MacAindra =cut
    Noted also, that using $' (and friends) incurs a lot of overhead.
    And, just because they're not here, use strict; use warnings.