Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Simple regex question

by zuma53 (Beadle)
on Sep 28, 2012 at 18:09 UTC ( [id://996261]=perlquestion: print w/replies, xml ) Need Help??

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

I am using \w+ to filter words in a syntax.
All letters: good Letters and numbers: good All numbers: bad
How can I set up a regex to say \w+ except \d+?

Replies are listed 'Best First'.
Re: Simple regex question
by BrowserUk (Patriarch) on Sep 28, 2012 at 18:13 UTC

    Almost exactly the way you described it:

    print "$_: ", /^\w+$/ && !/^\d+$/ ? 'Good' : 'Bad' for qw[ abc a1c 1b3 + 123 ];; abc: Good a1c: Good 1b3: Good 123: Bad

    Or if you insist on a single regex:

    print "$_: ", /^(?=.*[a-zA-z])\w+$/ ? 'Good' : 'Bad' for qw[ abc a1c 1 +b3 123 ];; abc: Good a1c: Good 1b3: Good 123: Bad

    Though it is doubtful if it is any more efficient; and is certainly less clear.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Simple regex question
by bart (Canon) on Sep 29, 2012 at 09:05 UTC
    /^(?=.*[a-z])\w+$/i
    (translation: all word characters, but at least a letter)

    You don't say if you think underscores are acceptable...

    /^(?=.*[a-z])(?!.*_)\w+$/i
    (translation: All word characters, but at least a letter, an no underscore)

    There.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://996261]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found