Re: case statement to validate input

From: laura_fairhead (laura_fairhead_at_INVALID.com)
Date: 10/13/03


Date: Mon, 13 Oct 2003 15:21:56 +0000

In article <987ib.147337$bo1.8607@news-server.bigpond.net.au>, John wrote:
>Hi,
>
>I'm trying to validate input using:
>
>case "$answer" in
>[a-zA-Z][0-9a-zA-Z]\{7\}
>echo "Correct input";;
>*)
>echo "Incorrect input";;
>esac
>
>I'm expecting input to be a char followed by up to 7 chars or digits.
>
>Can anyone suggest were I'm failing?

There is no closing bracket to delimit the case pattern but more
importantly the pattern won't match what you want - case statements
take a shell pattern (similar to a filename pattern) not a regular
expression so the modifier \{7\} won't be recognised. (As a regular
expression this actually says a character followed by 7 digits and
not 'up to' as you state in your description)

You should write it out in full;

case $answer in
[a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]\
[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]) echo "Correct input";;
*) echo "Incorrect input";;
esac

Instead of 7 characters allowing a variable number from 0 to 7
would be;

case $answer in
?????????*|""|[!a-zA-Z]*|?*[!0-9a-zA-Z]*) echo "Incorrect Input";;
*) echo "Correct Input";;
esac

byefornow
l

>
>Thanks
>
>
>

-- 
echo alru_aafriehdab@ittnreen.tocm |sed 's/\(.\)\(.\)/\2\1/g'


Relevant Pages

  • Re: Pattern Match
    ... I believe that the pattern Doug sent works. ... I have to find 3 digits followed by space and then 2 digits. ... wouldn't have thought possible; improve on PERL5 regexps! ... Jesse Houwing ...
    (microsoft.public.dotnet.languages.csharp)
  • modulo encrypt problem
    ... I am trying to get an extremely simple character string encryption function ... The pattern to be encrypted is a string of ASCII numeric digits. ...
    (comp.lang.c)
  • Re: replacing substring starting with a % sign
    ... rest of the pattern match the end or the start of the string respectively. ... then you do need the command substitution; ... it could be interpretted as an option by 'echo', if $comm contains ...
    (comp.unix.shell)
  • Re: Entropy in crystalization: up or down?
    ... Strings of digits without context are without ... have short and long blips, as in a Morse Code-type pattern, and these ... any string of digits in pi will be found within any ... artifact behind such patterns. ...
    (talk.origins)
  • Re: Trouble with $key to HASH when Numeric
    ... the end of a string -- in other words, this pattern will always match ... and capture either 5 digits or nothing; 2) the pattern is anchored to ... modifier, which you do not). ...
    (comp.lang.perl)