Need more better and efficient solutions for useing regular expressions to validate the user input in csh?

I need some comments to get a more better and efficient solution for validating the user  input in a  csh script.  

Here are my solutions

#last two digits of year (00..99)
if ( `echo $Yr | grep '^[0-9][0-9]$'` == '' ) then
        echo INVALID Yr;exit 1
#day of year (001..366)
 if ( `echo $Doy | grep '^[0-2][0-9]\{2\}$'` == '' && \
           `echo $Doy | grep '^3[0-6]\{2\}$'`     == '' || \
           $Doy == '000' ) then
        echo INVALID Doy;exit 1

 #4-character site ID (can contain only letters, numbers)
 if ( `echo $Site | grep '^[a-zA-Z0-9]\{4\}$'` == '' ) then
        echo INVALID SiteID;exit 1

#1-character session ID (can contain only 0 or letters a-x)
 if ( `echo $SessID | grep '^[0|a-xA-Z]$'` == ''  ) then
        echo INVALID SessID;exit 1

Any comments will be appreciated

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

* For the first expression, you can also use:
^[0-9]{2}$  , it's shorter

* For the day of year:

(if you want to take into account the leap years, you'd need to do more calculations)

if you want  1 ... 366 format you can use this regular expression:


This would allow more accurate checking of the day.

* The expression for the 1 char ID is wrong:
^[0|a-xA-Z]$   ---> change to this:   ^[0a-xA-X]$

JerryleoAuthor Commented:
Thanks for reply


It doesn't work. for example,
[jerry@beks-m04 ~]$ echo 203 | grep '^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$'
[jerry@beks-m04 ~]$

Yes, because you're using grep.
Try using egrep, that one does regular expression matching.
Or try grep -E

drk@nevermore / $ echo 203 | grep "^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$"
drk@nevermore / $ echo 203 | egrep "^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$"
drk@nevermore / $ echo 203 | grep -E "^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.