Regular expression, seemingly simple but

Hello,

I want to test a hour variable with an expression regular
The format is 00 01 02 03.......19 20 21 22 23
what follows in red doesn't work, it's clear 19 for example can't work.
Can you help me the right regular expression ?

case "$3" in
([0-2][0-3])
  # Nothing, OK !
  ;;
(*)
  echo 'Fatal, $3 = '"'$3'"', bad format' >&2
  #exit 1
  ;;

Try it without the open bracket.

The format of a case statement (in ksh at least) is:

case $var in
   match1) Action ;;
   match2) Action ;;
   *)   Action ;;
esac

I hope that this helps.

Robin
Liverpool/Blackburn
UK

To be pedantic: those patterns are not regular expressions. It's just sh pattern matching notation.

To be helpful: use multiple patterns.

case "$3" in
([01][0-9] | 2[0-3])
  # Nothing, OK !
  ;;
(*)
  echo 'Fatal, $3 = '"'$3'"', bad format' >&2
  #exit 1
  ;;

Regards,
Alister

---------- Post updated at 08:55 AM ---------- Previous update was at 08:52 AM ----------

The opening parenthesis is typically omitted, but it is allowed, even on ksh.

Regards,
Alister

2 Likes

That's me told then! Well, I'm happy to be corrected.

Robin

Thanks a lot Alister :slight_smile: Super

---------- Post updated at 10:44 AM ---------- Previous update was at 04:00 AM ----------

Thanks