Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Keep It Simple, Stupid
 
PerlMonks  

Re: general regex question

by fauria (Deacon)
on May 14, 2005 at 11:08 UTC ( [id://457083]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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 12: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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://457083]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.