... 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:
-
must contain only alphanumeric characters, with at least one alphabetic character and at least one numeric character; or
-
may contain any characters, but with at least one alphabetic character and at least one numeric character; or
-
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'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|