11

I simply need to know how to make a regex that accepts a number that has up to 2 digits

All I have just now is

^[0-9]{2}$

Which would match a number with exactly 2 digits, but I don't know how to specify "match a number which has up to 2 digits".

Also, if there is a way to make sure that this number isn't 0 then that would be a plus, otherwise I can check that with Bash.

Thanks ! :)

Note that the input variable comes from read -p "make a choice" Number

EDITING MY POST - SHOWING CODE IN CONTEXT :

while true; do
    read -p "Please key in the number of the engineer of your choice, or leave empty to select them all: " Engineer
    if [ -z "$Engineer" ]; then
        echo "No particular user specified, all engineers will be selected."
        UserIdSqlString="Transactions.Creator!=0 "
        break
    else
        if [[ $Engineer =~ ^[0-9]{1,2}$ && $Engineer -ne 0 ]]; then
            echo "If you want a specific engineer type their number otherwise leave blank"  
        else
            echo "yes"
            break
        fi
    fi
done
Bluz
  • 5,980
  • 11
  • 32
  • 40
  • Are you using `grep` or `sed` or something? – Mulan Nov 08 '13 at 16:50
  • I've edited my post. I don't mind involving grep or sed at all :) – Bluz Nov 08 '13 at 17:12
  • 1
    @Bluz, you have the `if-else` backward. `if [[ $Engineer =~ ^[0-9]{1,2}$ && $Engineer -ne 0 ]]; ` returns true when you have a non-zero two digit number set into `Engineer` so `echo "yes"` should follow this – iruvar Nov 08 '13 at 17:18
  • lol I am a doughnut...shows that I need a coffee break!! Thanks mate! :) – Bluz Nov 08 '13 at 17:20
  • Your test still has a problem when `Engineer`'s value is `08` or `09`. Please see my comment in 1_CR's answer below. – gniourf_gniourf Nov 08 '13 at 18:15

4 Answers4

10

the bash [[ conditional expression supports extended regular expressions.

[[ $number =~ ^[0-9]{,2}$ && $number -ne 0 ]]

or as the inimitable @gniourf_gniourf points out in his comments, the following is needed to handle numbers with leading zeroes correctly

[[ $number =~ ^[0-9]{,2}$ ]] && ((number=10#$number))
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • mmmhhh....doesn't seem to work :( I can add a zero. Note that the variable comes from read -p . I don't know if that is relevant but I'll edit my question just in case. – Bluz Nov 08 '13 at 17:00
  • @Bluz, have you tried `if [[ $number =~ ^[0-9]{,2}$ && $number -ne 0 ]]; then echo 'yes'; fi` after `read -p "make a choice" number`? – iruvar Nov 08 '13 at 17:04
  • yes I have, let me edit again to show you my code. Also, is that not supposed to be {1,2} instead of {,2} as you put in your expression ? Did you do that on purpose or is this a typo? I tried with {1,2} as well but still not working :( – Bluz Nov 08 '13 at 17:08
  • @Bluz, `{,2}` is as intended, that's an accepted form of "interval expression" – iruvar Nov 08 '13 at 17:13
  • 2
    Be aware that this solution is broken in the case when the value of `number` is `08` or `09`, since in the checking `$number -ne 0`, bash will try to interpret `08` or `09` as a number in radix 8 (because prefixed by a `0`) and this will throw an error. – gniourf_gniourf Nov 08 '13 at 18:11
  • 4
    Maybe `[[ $number =~ ^[[:digit:]]{1,2}$ ]] && ((number=10#$number))` would be better: it will assign to `number` its value in radix 10, even if there are leading zeros, and at the same time check it's not zero. `:)` – gniourf_gniourf Nov 08 '13 at 18:13
  • 1
    @gniourf_gniourf, looking forward to your book on `bash` ;-) Incorporated in the answer – iruvar Nov 08 '13 at 18:19
  • Thank you 1_CR! Though, I'm not planning to write any book yet, there's still so much to learn! – gniourf_gniourf Nov 08 '13 at 18:24
  • hey thanks guys for your additional comments and for going the extramile! I did actually notice the same thing and solved it by : $Engineer =~ ^[0-9]{1,2}$ && $Engineer -ne 0 && $Engineer =~ ^[^0] And because the maximum of digits allowed is 2 then 001 wouldn't work either :) – Bluz Nov 08 '13 at 18:30
7
^([1-9]|[1-9]{1}[0-9]{1})$

matches every number from 1,2,3...99

EverythingRightPlace
  • 1,197
  • 12
  • 33
6

The answer that I found is:

^[0-9]{1,2}$
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Bluz
  • 5,980
  • 11
  • 32
  • 40
0
#!/bin/bash
if [ "$1" -gt 0 ] 2>/dev/null ;then 
    echo "$1 is number." 
else 
    echo 'no.' 
fi 
Jagger Yu
  • 577
  • 4
  • 5