bash get a regular expression number up to 2 digits
Asked Answered
M

4

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
Milord answered 8/11, 2013 at 16:45 Comment(5)
Are you using grep or sed or something?Guesthouse
I've edited my post. I don't mind involving grep or sed at all :)Milord
@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 thisCompliance
lol I am a doughnut...shows that I need a coffee break!! Thanks mate! :)Milord
Your test still has a problem when Engineer's value is 08 or 09. Please see my comment in 1_CR's answer below.Sosanna
C
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))
Compliance answered 8/11, 2013 at 16:51 Comment(9)
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.Milord
@Bluz, have you tried if [[ $number =~ ^[0-9]{,2}$ && $number -ne 0 ]]; then echo 'yes'; fi after read -p "make a choice" number?Compliance
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 :(Milord
@Bluz, {,2} is as intended, that's an accepted form of "interval expression"Compliance
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.Sosanna
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. :)Sosanna
@gniourf_gniourf, looking forward to your book on bash ;-) Incorporated in the answerCompliance
Thank you 1_CR! Though, I'm not planning to write any book yet, there's still so much to learn!Sosanna
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 :)Milord
F
7
^([1-9]|[1-9]{1}[0-9]{1})$

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

Firetrap answered 8/11, 2013 at 16:51 Comment(0)
M
6

The answer that I found is:

^[0-9]{1,2}$
Milord answered 8/11, 2013 at 16:48 Comment(0)
B
0
#!/bin/bash
if [ "$1" -gt 0 ] 2>/dev/null ;then 
    echo "$1 is number." 
else 
    echo 'no.' 
fi 
Bergren answered 30/12, 2018 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.