Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

DD/MM/YYYY regex

DD/MM/YYYY regex

DD/MM/YYYY regex

what is wrong in that regex that it does not work in bash, ksh, grep?

CODE --> regex


It does the job well testing on regexr but does not work in bash:

CODE --> error

root@deb1:~# [ "${DD}" =~ ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$ ] && echo date
bash: syntax error near unexpected token `(' 

RE: DD/MM/YYYY regex


=~ is performed only inside [[ .. ]].

Then you could give us a few example values of $DD you wish to match, so we have a starting point before debugging all that expression...


RE: DD/MM/YYYY regex

Like in subject, so for example 29/09/1972

RE: DD/MM/YYYY regex


Only that ? Ok. Now I had time for a deeper look.

Bash has no idea about \d, only [[:digit:]] works :

CODE --> Bash

[[ "29/09/1972" =~ ^(((0[1-9]|[12][[:digit:]]|3[01])/(0[13578]|1[02])/((19|[2-9][[:digit:]])[[:digit:]]{2}))|((0[1-9]|[12][[:digit:]]|30)/(0[13456789]|1[012])/((19|[2-9][[:digit:]])[[:digit:]]{2}))|((0[1-9]|1[[:digit:]]|2[0-8])/02/((19|[2-9][[:digit:]])[[:digit:]]{2}))|(29/02/((1[6-9]|[2-9][[:digit:]])(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$ ]] && echo date 

Similar problem in grep with the default basic regular expressions, just it requires some additional escaping :


grep '^\(\(\(0[1-9]\|[12][[:digit:]]\|3[01]\)/\(0[13578]\|1[02]\)/\(\(19\|[2-9][[:digit:]]\)[[:digit:]]\{2\}\)\)\|\(\(0[1-9]\|[12][[:digit:]]\|30\)/\(0[13456789]\|1[012]\)/\(\(19\|[2-9][[:digit:]]\)[[:digit:]]\{2\}\)\)\|\(\(0[1-9]\|1[[:digit:]]\|2[0-8]\)/02/\(\(19\|[2-9][[:digit:]]\)[[:digit:]]\{2\}\)\)\|\(29/02/\(\(1[6-9]\|[2-9][[:digit:]]\)\(0[48]\|[2468][048]\|[13579][26]\)\|\(\(16\|[2468][048]\|[3579][26]\)00\)\)\)\)$' <<< "29/09/1972" && echo date 

If your grep supports extended regular expressions, then you can use the same regular expression as in Bash :


grep -E '^(((0[1-9]|[12][[:digit:]]|3[01])/(0[13578]|1[02])/((19|[2-9][[:digit:]])[[:digit:]]{2}))|((0[1-9]|[12][[:digit:]]|30)/(0[13456789]|1[012])/((19|[2-9][[:digit:]])[[:digit:]]{2}))|((0[1-9]|1[[:digit:]]|2[0-8])/02/((19|[2-9][[:digit:]])[[:digit:]]{2}))|(29/02/((1[6-9]|[2-9][[:digit:]])(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$' <<< "29/09/1972" && echo date 

If your grep supports Perl regular expressions, then you can use your original regular expression, with \d :


grep -P '^(((0[1-9]|[12]\d|3[01])/(0[13578]|1[02])/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)/(0[13456789]|1[012])/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])/02/((19|[2-9]\d)\d{2}))|(29/02/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$' <<< "29/09/1972" && echo date 

Note that escaping the slashes ( / ) is required by the languages where regular expression literals are delimited with slashes. So in none of the above cases.


RE: DD/MM/YYYY regex

thank you.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close