Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DD/MM/YYYY regex 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
what is wrong in that regex that it does not work in bash, ksh, grep?

Code:
^(((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))))$

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

Code:
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 `('
 
Hi

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

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...


Feherke.
feherke.ga
 
Like in subject, so for example 29/09/1972
 
Hi

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

Bash has no idea about [tt]\d[/tt], only [tt][[:digit:]][/tt] works :
Bash:
[teal][[[/teal] [i][green]"29/09/1972"[/green][/i] [teal]=~ ^((([/teal][purple]0[/purple][teal][[/teal][purple]1[/purple]-[purple]9[/purple][teal]]|[[/teal][purple]12[/purple][teal]][[:[/teal]digit[teal]:]]|[/teal][purple]3[/purple][teal][[/teal][purple]01[/purple][teal]])/([/teal][purple]0[/purple][teal][[/teal][purple]13578[/purple][teal]]|[/teal][purple]1[/purple][teal][[/teal][purple]02[/purple][teal]])/(([/teal][purple]19[/purple][teal]|[[/teal][purple]2[/purple]-[purple]9[/purple][teal]][[:[/teal]digit[teal]:]])[[:[/teal]digit[teal]:]][/teal]{[purple]2[/purple]}[teal]))|(([/teal][purple]0[/purple][teal][[/teal][purple]1[/purple]-[purple]9[/purple][teal]]|[[/teal][purple]12[/purple][teal]][[:[/teal]digit[teal]:]]|[/teal][purple]30[/purple][teal])/([/teal][purple]0[/purple][teal][[/teal][purple]13456789[/purple][teal]]|[/teal][purple]1[/purple][teal][[/teal][purple]012[/purple][teal]])/(([/teal][purple]19[/purple][teal]|[[/teal][purple]2[/purple]-[purple]9[/purple][teal]][[:[/teal]digit[teal]:]])[[:[/teal]digit[teal]:]][/teal]{[purple]2[/purple]}[teal]))|(([/teal][purple]0[/purple][teal][[/teal][purple]1[/purple]-[purple]9[/purple][teal]]|[/teal][purple]1[/purple][teal][[:[/teal]digit[teal]:]]|[/teal][purple]2[/purple][teal][[/teal][purple]0[/purple]-[purple]8[/purple][teal]])/[/teal][purple]02[/purple][teal]/(([/teal][purple]19[/purple][teal]|[[/teal][purple]2[/purple]-[purple]9[/purple][teal]][[:[/teal]digit[teal]:]])[[:[/teal]digit[teal]:]][/teal]{[purple]2[/purple]}[teal]))|([/teal][purple]29[/purple][teal]/[/teal][purple]02[/purple][teal]/(([/teal][purple]1[/purple][teal][[/teal][purple]6[/purple]-[purple]9[/purple][teal]]|[[/teal][purple]2[/purple]-[purple]9[/purple][teal]][[:[/teal]digit[teal]:]])([/teal][purple]0[/purple][teal][[/teal][purple]48[/purple][teal]]|[[/teal][purple]2468[/purple][teal]][[/teal][purple]048[/purple][teal]]|[[/teal][purple]13579[/purple][teal]][[/teal][purple]26[/purple][teal]])|(([/teal][purple]16[/purple][teal]|[[/teal][purple]2468[/purple][teal]][[/teal][purple]048[/purple][teal]]|[[/teal][purple]3579[/purple][teal]][[/teal][purple]26[/purple][teal]])[/teal][purple]00[/purple][teal]))))[/teal]$ [teal]]] &&[/teal] echo date

Similar problem in [tt]grep[/tt] with the default basic regular expressions, just it requires some additional escaping :
Code:
grep [i][green]'^[/green][/i][lime]\(\(\([/lime][i][green]0[1-9][/green][/i][lime]\|[/lime][i][green][12][[:digit:]][/green][/i][lime]\|[/lime][i][green]3[01][/green][/i][lime]\)[/lime][i][green]/[/green][/i][lime]\([/lime][i][green]0[13578][/green][/i][lime]\|[/lime][i][green]1[02][/green][/i][lime]\)[/lime][i][green]/[/green][/i][lime]\(\([/lime][i][green]19[/green][/i][lime]\|[/lime][i][green][2-9][[:digit:]][/green][/i][lime]\)[/lime][i][green][[:digit:]][/green][/i][lime]\{[/lime][i][green]2[/green][/i][lime]\}\)\)\|\(\([/lime][i][green]0[1-9][/green][/i][lime]\|[/lime][i][green][12][[:digit:]][/green][/i][lime]\|[/lime][i][green]30[/green][/i][lime]\)[/lime][i][green]/[/green][/i][lime]\([/lime][i][green]0[13456789][/green][/i][lime]\|[/lime][i][green]1[012][/green][/i][lime]\)[/lime][i][green]/[/green][/i][lime]\(\([/lime][i][green]19[/green][/i][lime]\|[/lime][i][green][2-9][[:digit:]][/green][/i][lime]\)[/lime][i][green][[:digit:]][/green][/i][lime]\{[/lime][i][green]2[/green][/i][lime]\}\)\)\|\(\([/lime][i][green]0[1-9][/green][/i][lime]\|[/lime][i][green]1[[:digit:]][/green][/i][lime]\|[/lime][i][green]2[0-8][/green][/i][lime]\)[/lime][i][green]/02/[/green][/i][lime]\(\([/lime][i][green]19[/green][/i][lime]\|[/lime][i][green][2-9][[:digit:]][/green][/i][lime]\)[/lime][i][green][[:digit:]][/green][/i][lime]\{[/lime][i][green]2[/green][/i][lime]\}\)\)\|\([/lime][i][green]29/02/[/green][/i][lime]\(\([/lime][i][green]1[6-9][/green][/i][lime]\|[/lime][i][green][2-9][[:digit:]][/green][/i][lime]\)\([/lime][i][green]0[48][/green][/i][lime]\|[/lime][i][green][2468][048][/green][/i][lime]\|[/lime][i][green][13579][26][/green][/i][lime]\)\|\(\([/lime][i][green]16[/green][/i][lime]\|[/lime][i][green][2468][048][/green][/i][lime]\|[/lime][i][green][3579][26][/green][/i][lime]\)[/lime][i][green]00[/green][/i][lime]\)\)\)\)[/lime][i][green]$'[/green][/i] [teal]<<<[/teal] [i][green]"29/09/1972"[/green][/i] [teal]&&[/teal] echo date

If your [tt]grep[/tt] supports extended regular expressions, then you can use the same regular expression as in Bash :
Code:
grep -E [i][green]'^(((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))))$'[/green][/i] [teal]<<<[/teal] [i][green]"29/09/1972"[/green][/i] [teal]&&[/teal] echo date

If your [tt]grep[/tt] supports Perl regular expressions, then you can use your original regular expression, with [tt]\d[/tt] :
Code:
grep -P [i][green]'^(((0[1-9]|[12][/green][/i][lime]\d[/lime][i][green]|3[01])/(0[13578]|1[02])/((19|[2-9][/green][/i][lime]\d[/lime][i][green])[/green][/i][lime]\d[/lime][i][green]{2}))|((0[1-9]|[12][/green][/i][lime]\d[/lime][i][green]|30)/(0[13456789]|1[012])/((19|[2-9][/green][/i][lime]\d[/lime][i][green])[/green][/i][lime]\d[/lime][i][green]{2}))|((0[1-9]|1[/green][/i][lime]\d[/lime][i][green]|2[0-8])/02/((19|[2-9][/green][/i][lime]\d[/lime][i][green])[/green][/i][lime]\d[/lime][i][green]{2}))|(29/02/((1[6-9]|[2-9][/green][/i][lime]\d[/lime][i][green])(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$'[/green][/i] [teal]<<<[/teal] [i][green]"29/09/1972"[/green][/i] [teal]&&[/teal] 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.


Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top