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

Posted on 2006-03-29
Last Modified: 2010-04-22
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

Question by:Jerryleo
    LVL 5

    Expert Comment

    * 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]$


    Author Comment

    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 ~]$

    LVL 5

    Accepted Solution

    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])$"

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now