问题
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