Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: regex help

by AnomalousMonk (Monsignor)
on Oct 06, 2013 at 04:39 UTC ( #1057110=note: print w/ replies, xml ) Need Help??


in reply to regex help

... must contain a mix of both letters and numbers.
... good words are the words that are a mix of letters and numbers.

The specification and example in the OP is a bit unclear to me, but, taken with some of the other replies, leads me to think that a "word" is a string that either:

  1. must contain only alphanumeric characters, with at least one alphabetic character and at least one numeric character; or
  2. may contain any characters, but with at least one alphabetic character and at least one numeric character; or
  3. may contain any characters, but with at least one contiguous alphabetic and numeric character pair in any order.

The other replies seem to lean toward alternatives 2 and 3 above. My own first guess was for alternative 1, as in the last code examples below:

>perl -wMstrict -le "my @lines = qw(abc 345 a1 1a a1a 1a1 abc1 1abc a1==a1 a==1); printf '@lines: '; printf qq{'$_' } for @lines; print qq{\n}; ;; printf 'and 1: '; printf qq{'$_' } for grep { /[[:alpha:]]/ && /\d/ } @lines; print ''; ;; printf 'regex 1: '; printf qq{'$_' } for grep m{ [[:alpha:]] \d | \d [[:alpha:]] }xms, @l +ines; print qq{\n}; ;; ;; printf 'and 2: '; printf qq{'$_' } for grep { !/[^[:alnum:]]/ && /[[:alpha:]]/ && /\d/ +} @lines; print ''; ;; my $al_num = qr{ [[:alpha:]] \d | \d [[:alpha:]] }xms; printf 'regex 2: '; printf qq{'$_' } for grep m{ \A [[:alnum:]]* $al_num [[:alnum:]]* \z +}xms, @lines; print qq{\n}; ;; ;; printf '@lines as was: '; printf qq{'$_' } for @lines; " @lines: 'abc' '345' 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' 'a1==a1' 'a==1 +' and 1: 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' 'a1==a1' 'a==1' regex 1: 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' 'a1==a1' and 2: 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' regex 2: 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' @lines as was: 'abc' '345' 'a1' '1a' 'a1a' '1a1' 'abc1' '1abc' 'a1==a1 +' 'a==1'


Comment on Re: regex help
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057110]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-08-30 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls