use strict; use warnings; my $string; while (1){ print "Please enter 1-5 letters: "; chomp ($string = <STDIN>); last if $string =~ m/^[a-z]{1,5}$/i; # last if $string =~ m/^\w{1,5}$/; print "that is wrong....try again: "; }
I think the problem was not the pattern so much as the rest of the logic. The pattern you have will match a lower- or upper-case letter of the alphabet one or five times. I can't see how anchoring it to the start or end of the $string would help anything other than perhaps internal efficiency, for which I'd anchor it at both ends (^ and $ for start and end). I would suggest using the /i modifier, so instead of /[a-zA-Z]/ to match any letter, you could /[a-z]/i. But that's not really 'any letter' -- it's just a to z, which may ignore accented and other UTF-8 characters. You can find better options in perldoc perlre -- see esp. the POSIX section. Your original code also seemed to be warning of an error before taking input, but I guess that's a cut-n-paste issue. Hth lee > From: M K Scott [mailto:[EMAIL PROTECTED] > > I have tried that to no avail. I have also tried a simple > match of !~ m/(m|f){1}/ and even put in the code you > suggested to read !~ m/^(m|f){1}$/ but this still doesn't > work properly. Input of "d" or "T" will work to say it is > incorrect and input of "m" or "f" will be accepted but I was > under the impression that the {1} would limit it to only > accept a single character while in practice it still accepts > "ff" and "mmm". Any ideas? > > One further question though, an example question I am doing > asks for a text file to be read in and the number of digits > to be counted (ie 3 "1"'s, 6 "2"'s etc) and I can read input > in and do a 'getc' and pattern match that against a hash > containing word references to the numbers and then add one to > a count but is this the best way to do this? > > Thanks again, > Mark > > ________________________________ > > From: John W. Krahn [mailto:[EMAIL PROTECTED] > Sent: Wed 20/09/2006 01:12 > To: Perl Beginners > Subject: Re: Newbie Question > > > > M K Scott wrote: > > Hi all, > > Hello, > > > Please forgive the newbie nature of this question but i'm just > > starting off teaching myself perl from scratch and so need a little > > clarification. > > > > I am trying to put together a script to do pattern matching > and while > > I can get the basic syntax alright it doesn't seem to be working as > > expected. For one example I need to match a user input > and make sure > > it is a certain length (i.e. doesn't exceed a certain number of > > characters) and this is the code so far: > > > > while ($string != m/[a-zA-Z]{1,5}/ ) > > { print("that is wrong....try again: "); > > chomp ($string = <STDIN>); } > > You need to anchor the pattern like /^[a-zA-Z]{1,5}$/ -- http://www.bbc.co.uk/ This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated. If you have received it in error, please delete it from your system. Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately. Please note that the BBC monitors e-mails sent or received. Further communication will signify your consent to this. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>