Exact Regex expression for floating point number is not working

久未见 提交于 2020-05-30 09:55:04

问题


Exact regex expression for floating point number is not working and it execute both integer and floating point also.It should not work if the input given does not have decimal point.Please help me

#!/bin/bash
echo "Enter version code" 
read versionName
if ! [[ "$versionName" =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then
echo "Sorry decimal numbers only"
echo "$versionName"
else
sudo  sed 's/\(versionName[[:space:]]*\)"[0-9.]*"/\1"'"${versionName}"'"/' test.txt
fi

回答1:


Use this: ^[+-]?[0-9]+\.[0-9]+$

                   ^ no "?" -- making the decimal point compulsory

                          ^ "+" instead of "*" matching one or more, not 0 or more because otherwise "12." would match too (unless you do want it to match too)



回答2:


In your initial regex ^[+-]?[0-9]+\.?[0-9]*$ the '?' behind the '.' makes it so that the regex accepts zero or 1 dots. What you want is: ^[+-]?[0-9]+\.[0-9]+$ Note the '+' at the end of this regex which also enforces 1 or more digit follows the dot. For example "1." wont be accepted but "1.234" will be accepted.



来源:https://stackoverflow.com/questions/61206182/exact-regex-expression-for-floating-point-number-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!