Assembling the Pieces of a Regular Expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assembling the Pieces of a Regular Expression
# 1  
Old 08-01-2012
Assembling the Pieces of a Regular Expression

Hello all.

I'm scripting in ksh and trying to put together a regular expression. I think my logic is sound, but I'm doing the head-against-the-wall routine while trying to put the individual pieces together. Can anybody lend some suggestions to the below problem?

I'm taking a date in the format of "DD-MMM-YY" as a parameter for a script. I want to user-proof this as much as possible, so "01-AUG-13" is valid but nonsense (i.e. "41-MAK-0G") gets rejected. This is a job for regular expressions.

I've broken down the "01-AUG-13" example into five separate expressions to be evaluated, and I think my regex logic is sound. (But if it isn't, please let me know!)

Part 1="01"
Part 2="-"
Part 3="AUG"
Part 4="-"
Part 5="13"

Part 1 translates into:
"match two digits, value between 01 and 31",
Which further translates into:
"match first digit, with value between 0-2, one time, then match second digit value between 0-9 one time |||OR||| match first digit 3 one time (because if we missed the first match of 0-2, then it has to be this and only this), then match second digit, with value of 0 or 1, one time."

Parts 2 & 4 translate into "exactly one dash here"

Part 3 becomes "match exactly one 3 char month name out of the valid set of month name values".

Part 5 is "match exactly two digits".

I have coded the regular expressions for these values as follows:
Part 1:
Code:
 
([0-2][0-9]|[3][0-1])

Parts 2 and 4:
Code:
 
[-]

Part 3:
Code:
 
grep -i [JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC]

I couldn't think of a simple way to do this without using grep -i; what are some alternatives?

Part 5:
Code:
 
[0-9][0-9]

So, all of those pieces seem sound, individually. The function doesn't work once I combine them, however. One of my many iterations (and probably the simplest) is:

Code:
 
VARIABLE=01-AUG-13
print $VARIABLE | grep -i -E "([0-2][0-9]|[3][0-1])[-][JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC][-][0-9][0-9]"

I am doing something wrong, but I honestly don't know what it might be. Can anyone lend some suggestions on how I can properly write this regular expression?

Any help is appreciated. Thank you!
# 2  
Old 08-01-2012
Code:
grep -i -E "([0-2][0-9]|3[01])-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-[0-9][0-9]"

This isn't completely fool-proof. This regex would match 31-Feb-12 or 00-APR-00. Only thing is, we've narrowed down the possible causes of errors.

A suggestion - If you've GNU date, you could validate date like this:

Code:
x="31-FEB-12"
date -d $x
if [ $? -ne 0 ]
then
  echo "Not a valid date"
else
  echo "Valid date"
fi


Last edited by balajesuri; 08-01-2012 at 01:49 PM..
# 3  
Old 08-01-2012
balajesuri,

Thank you for your assistance. The code you supplied...
Code:
 
grep -i -E "([0-2][0-9]|3[01])-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-[0-9][0-9]"

...worked perfectly for me!

I guess my problem was a syntax error; I notice you did not enclose the "-" within brackets.

Thank you for pointing out the potential for illogical dates such as 31-FEB. I know my code isn't 100% fool-proof, but I'm writing my script for (reasonably) experienced users, so hopefully I don't have to code against something that foolish. Smilie

I like your suggestion to use the date -d. It appears I don't have GNU date available to me, though. That would have been a beautiful solution to the problem, however!

Thank you very much for your assistance! I really appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression

Hello Gurus, I am looking for regular expressions for awk to filter on second column 2nd and 3rd digit Using in code something like: awk '{if ( $2 == "0::" ) print} source file: 0 0:0:0 FC 15 normal 559104 51200 0:3:1* 1:3:1 600 1 0:0:1 FC 15 normal ... (2 Replies)
Discussion started by: vishalgoyal
2 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Shell Programming and Scripting

Regular Expression

Hello, I want to extract text between <td class="di_resultscolumnheader"> and </td>. I wrote the below code to extract text. But I am able to extract the text for the first match only. Can some one help me in this? Thanks in advance. Code: if ($line =~ /<td... (5 Replies)
Discussion started by: koneru_18
5 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

Regular expression Help

Hi What is the meaning of this in regular expression $k =~ s/^\s*//; Plz explain (3 Replies)
Discussion started by: Harikrishna
3 Replies

7. Shell Programming and Scripting

Regular expression

Hi I have to extract the first field and the last %field of the following out put.. /home (/abc/def/bhd ) : 522328 total allocated Kb 319448 free allocated Kb ... (2 Replies)
Discussion started by: Harikrishna
2 Replies

8. UNIX for Dummies Questions & Answers

Regular expression help

HI All, I want to list a file with the below format : testfile_nnnnn.xxxx where n and x can be any digit 0-9. n repeats 5 times and x 4 times... I tried with something like below: ls -l testfile_/\{5\}/* to start with but its not working. Please could anyone help? Thanks D (1 Reply)
Discussion started by: deepakgang
1 Replies

9. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question