我试图在korn shell中创建一个正则表达式模式。我是这个korn shell脚本及其正则表达式的新手。
以下情况需要正则表达式:
allowed values
如下:
80,01,00
80,00
01
00,80,01,02
not allowed values
如下:
80, --> with comma in the end.
,01, --> comma at the start and end.
,00 --> comma at the start.
8 --> with only one digit
800 --> with three digits
我尝试了以下正则表达式模式:
^(*([0-9][0-9][,])+([0-9][0-9])*([,][0-9][0-9]))$
,脚本的片段如下:
if [[ $END_POINT_LIST =~ ^(*([0-9][0-9][,])+([0-9][0-9])*([,][0-9][0-9]))$ ]]; then
echo "Input validation passed!"
export RETURN_CODE=16
exit 16
else
echo "[FATAL] The parameter doesn't match the expected pattern."
export RETURN_CODE=16
exit 16
fi
;;
上面的正则表达式模式没有按预期工作。
我无法在正则表达式中弄错。
任何指出错误的建议都会有很大的帮助。
谢谢!
修改
除了@degant的接受答案,我想提及以下内容。
在问题中发布的正则表达式中的错误是,
字符* and +
位于模式列表的开头。
如果更改为将* and +
放在模式列表之后,它可以正常工作。
以下更正的正则表达式也可以正常工作:
^(([0-9][0-9][,])*([0-9][0-9])+([,][0-9][0-9])*)$
答案 0 :(得分:3)
正则表达式匹配用逗号分隔的数字,并确保逗号不会出现在前端或末尾:
^\d{2}(,\d{2})*$
如果shell不支持\d
和{2}
:
^[0-9][0-9](,[0-9][0-9])*$
编辑:根据@Doqnach建议