• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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
 endif
 
#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
 endif

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

#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
 endif

Any comments will be appreciated

Thanks
0
Jerryleo
Asked:
Jerryleo
  • 2
1 Solution
 
Dragon_KromeCommented:
* 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:

^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$

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

0
 
JerryleoAuthor Commented:
Thanks for reply

>^(36[0-6]|3[0-5][0-9]|[1-2][0-9]{2}|[1-9][0-9]|[1-9])$

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

0
 
Dragon_KromeCommented:
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])$"
203
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])$"
203
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now