terdon has corrected your syntax, but your regular expression is wrong:
[0-9]*\.?[0-9]*
All quantifiers there (*
, ?
) mean all parts of the expression are optional. That means your regex will match every string, including strings that are empty and strings that have no digits.
To match a float number, you need to match at least one digit.
([0-9]+\.?[0-9]*)|([0-9]*\.[0-9]+)
That matches some digits with an optional decimal point and optional digits (example: 3.14 or 42), or some optional digits but a required decimal point and required digits (example: .1234 or 3.14).
It is not anchored, so the string "PI starts with 3.14 and continues" will match.
Testing:
for n in "" "no digits" 42 3.14 "this is .1234 go"; do
if [[ $n =~ ([0-9]+\.?[0-9]*)|([0-9]*\.[0-9]+) ]]; then
echo "yes -- $n -- ${BASH_REMATCH[0]}"
fi
done
yes -- 42 -- 42
yes -- 3.14 -- 3.14
yes -- this is .1234 go -- .1234