Re: regular expression



Krishna.Thotakura@xxxxxxxxxxxxxxxx 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
.



Relevant Pages

  • Re: Parsing a string into an array
    ... pragmas to let perl help you find mistakes. ... split Splits a string into a list of strings and returns ... The first argument to splitis a regular expression pattern. ...
    (perl.beginners)
  • Re: Metacharacter problem in regexp
    ... wrong with regular expressions in Perl 5 and how are they going to look ... interpolation you've asked about and the klunky workaround I've sent ... any piece of program is a string. ... string as a regular expression. ...
    (perl.beginners)
  • Re: A way of checking if a string contains a number
    ... I'm new to python, but I have used a fair bit of C and Perl ... All I am trying to do is detect if there is a number in a string. ... Is there a way I can drop the regular expression module and just use ...
    (comp.lang.python)
  • Re: Splitting on =
    ... >>> How do you split a string on the equality character? ... > a single string. ... it treats the first argument as a regular expression ... use Perl; ...
    (perl.beginners)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)