Re: regular expression



Although, i read about negated versions of lookahead and lookbehind
operators, i didnt really understand how to use them. Now, i get it.

Thanks John.






"John W. Krahn" <krahnj@xxxxxxxxx>
12/29/2005 12:47 PM


To: Perl Beginners <beginners@xxxxxxxx>
cc:
Subject: 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

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>






*************************************************************************
This communication, including attachments, is
for the exclusive use of addressee and may contain proprietary,
confidential and/or privileged information. If you are not the intended
recipient, any use, copying, disclosure, dissemination or distribution is
strictly prohibited. If you are not the intended recipient, please notify
the sender immediately by return e-mail, delete this communication and
destroy all copies.
*************************************************************************



Relevant Pages

  • Chopping off first&last character in a string
    ... is there an equal but opposite function to chop in Perl? ... I'd like to remove the first and last character of a string and am looking ... If you are not the intended recipient of ...
    (perl.beginners)
  • Re: Pattern Matching
    ... John W. Krahn wrote: ... > This is stored as a string in a variable. ... > I am unable to get a pattern to try pattern matching and spliting ... Perl isn't a toolbox, but a small machine shop where you can special-order ...
    (perl.beginners)
  • Re: Split a string in perl
    ... John wrote: ... The String is " 0000|Null". ... proj= 0000, desc=Null ... So they all work on my system (Perl 5.8.8). ...
    (comp.lang.perl.misc)
  • Re: before -
    ... my $string = "75664545-bookings"; ... John ... use Perl; ...
    (perl.beginners)
  • Re: Need help with parser unix script
    ... > according to String in line i. ... John ... use Perl; ...
    (comp.unix.admin)