regex - bash 获取最多 2 位数的正则表达式编号

标签 regex bash digit

我只需要知道如何制作一个接受最多 2 位数字的正则表达式

我现在只有

^[0-9]{2}$

这将匹配一个恰好 2 位数字的数字,但我不知道如何指定“匹配最多 2 位数字的数字”。

另外,如果有办法确保这个数字不为 0,那将是一个加号,否则我可以用 Bash 来检查。

谢谢! :)

注意输入变量来自read -p "make a choice"Number

编辑我的帖子 - 在上下文中显示代码:

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

最佳答案

庆典 [[条件表达式支持扩展的正则表达式。

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

或者正如无与伦比的@gniourf_gniourf 在他的评论中指出的那样,需要以下内容才能正确处理带有前导零的数字

[[ $number =~ ^[0-9]{,2}$ ]] && ((number=10#$number))

关于regex - bash 获取最多 2 位数的正则表达式编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19864295/

相关文章:

java - Java 中的字符测试器(大写 A-Z,小写 a-z,数字 0-9,其他)

regex - 你能解释一下这个正则表达式吗

bash - Bash 脚本算术语法错误

c++ - 为什么没有为 C++14 数字分隔符选择空格字符?

linux - 脚本测试以查看工具是否已加载?

linux - 在 linux shell 中运行 MongoDB 命令的错误替换

c# - .NET 中的数字分组

regex - gawk - 提取文本并将其放在同一行

java - 为什么 Matcher.find() 在 Matcher.lookingAt() 之后运行时返回 false?

regex - 正则表达式将第一个单词与每行中的一个字符匹配