[EMAIL PROTECTED] wrote:
> I am looking for help on a PERL regular expression that can do the 
> following:
> 
> 1. Make sure that the string is 20 chars or else match should fail.
> 2. The string can have word characters or spaces or  comma(,), hyphen(-), 
> ampersand(&) characters.
> 3. If the string contains only spaces, then match should fail.
> 
> Any ideas on how this can be done using just one regular expression ? 

This may do what you want:

$ perl -le'
my @array = (
    q/                    /,
    q/ab def,ghi-jkl&mn_pq/,
    q/abcdefghi^jklmnopqrs/,
    q/abcd/,
    q/abcdefghijklmnopqrstuvwxyz/,
    );

for ( @array ) {
    print "\047$_\047 is ", /\A(?!\s{20})[\w\s,&-]{20}\z/ ? "" : "NOT ", 
"valid";
    }
'
'                    ' is NOT valid
'ab def,ghi-jkl&mn_pq' is valid
'abcdefghi^jklmnopqrs' is NOT valid
'abcd' is NOT valid
'abcdefghijklmnopqrstuvwxyz' is NOT valid



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to