Bash RegEx 从用户输入检查浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13790763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash RegEx to check floating point numbers from user input
提问by Sam_Web
I'm relatively new to bash programming and i am currently creating a simple calculator.
我对 bash 编程比较陌生,目前正在创建一个简单的计算器。
It needs to use floating point numbers and check they are so.
它需要使用浮点数并检查它们是否如此。
I have a checkNumbers function:
我有一个 checkNumbers 函数:
function checkNumber {
regExp=[0-9]
if [ $testNo =~ $regExp ]
then
echo "That is a number!"
let check=1
else
echo "Damn! Not A Number!"
fi
}
where i get the user to input a number like this:
我让用户输入这样的数字:
while [ $check -eq 0]
do
echo "Please enter a number
read testNo
echo ""
checkNumber
done
This does not work, i get this error:
这不起作用,我收到此错误:
./calculator.sh: line 39: [: =~: binary operator expected
./calculator.sh: 第 39 行: [: =~: 预期的二元运算符
line 39 is:
第 39 行是:
if [ $testNo =~ $regExp ]
I have tried lots of Regular Expressions like:
我尝试了很多正则表达式,例如:
^*[0-9]*$
and
和
^*[0-9]\.[0.9]*$
etc etc.
等等等等
also, i have tied other ways of performing the check:
此外,我还绑定了其他执行检查的方法:
case $testNo
in
^*[0-9]*$) echo "That is a number!";;
*) echo "Error! Not a number!";;
esac
and
和
if [ $testNo = ^*[0-9]*$ ]
then
echo "etc etc"
else
echo "oops"
fi
I also need this to work with floating point numbers.
我也需要它来处理浮点数。
could someone please let me know how i go about this?
有人可以让我知道我是怎么做的吗?
回答by Chris Seymour
This regex ^[-+]?[0-9]+\.?[0-9]*$will match only digits with an optional .:
此正则表达式^[-+]?[0-9]+\.?[0-9]*$将仅匹配带有可选的数字.:
$ echo 30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
Match
$ echo 30.10 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
Match
$ echo 30. | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
Match
$ echo +30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
Match
$ echo -30 | grep -Eq '^[-+]?[0-9]+\.?[0-9]*$' && echo Match
Match
I think when you tried ^*[0-9]you wanted ^[0-9]*
我^*[0-9]想当你尝试的时候你想要^[0-9]*
Rexeplanation:
重新解释:
^ # Match start of string
[-+]? # Match a leading + or - (optional)
[0-9]+ # Match one or more digit
\.? # Match a literal . (optional, escaped)
[0-9]* # Match zero or more digits
$ # Match the end of the string
Note: this matches numbers followed by a .like 30., not sure if this is acceptable for you.
注意:这匹配数字后跟一个.like 30.,不确定这是否适合您。
Edit: Don't quote the regex
编辑:不要引用 regex
testNo=30.00
if [[ $testNo =~ ^[+-]?[0-9]+\.?[0-9]*$ ]]; then
echo Match
fi
>>> Match
回答by Mat
To use that type of feature, you need the [[ ... ]]version of the conditional. [is the "old" testcommand and doesn't handle regular expressions at all.
要使用这种类型的功能,您需要[[ ... ]]条件的 版本。[是“旧”test命令,根本不处理正则表达式。
#! /bin/bash
function checkNumber {
regExp='^[+-]?([0-9]+\.?|[0-9]*\.[0-9]+)$'
if [[ $testNo =~ $regExp ]]
then
echo "That is a number!"
let check=1
else
echo "Damn! Not A Number!"
fi
}
testNo=1
checkNumber
testNo=-1.2
checkNumber
testNo=+.2
checkNumber
testNo=+0.
checkNumber
testNo=a
checkNumber
testNo=hello2you
checkNumber
$ ./t.sh
That is a number!
That is a number!
That is a number!
That is a number!
Damn! Not A Number!
Damn! Not A Number!
See What is the difference between test, [ and [[ ?.
An explanation on the regex:
关于正则表达式的解释:
^ Anchor at start of string
$ Anchor at end of string
These two make the regex match the whole string passed, partial matches are not allowed.
这两个使正则表达式匹配传递的整个字符串,不允许部分匹配。
[+-]
matches either +or -.
匹配+或-。
[+-]?
makes that part optional, so the above matches exactly +, -or nothing at all.
使该部分可选,因此上述内容完全匹配+,-或者根本不匹配。
Then there's an alternation (part1|part2)which will match if part1or part2matches.
然后有一个交替(part1|part2)匹配 ifpart1或part2matches 。
Part one is:
第一部分是:
[0-9]+\.?
which matches one or more (+) digits (but not zero digits/empty set) and an optional .. This handles numbers of the form 123and 534.. But not just ..
它匹配一个或多个 ( +) 数字(但不是零数字/空集)和一个可选的.. 这处理形式123和的数字534.。但不仅如此.。
Part two is:
第二部分是:
[0-9]*\.[0-9]+
This matches zero or more (*) digits, followed by a ., followed by one or more digits. This matches all other floats like 1.3or .543(without exponent notation), but still excludes just ..
这匹配零个或多个 ( *) 数字,后跟 a .,后跟一个或多个数字。这匹配所有其他浮点数,如1.3or .543(没有指数符号),但仍然只排除..
回答by Donovan
#!/bin/bash
#script to validate
while [ true ]; do
clear
echo "Introduce the numeric value: "
read -r var
if [[ $var =~ ^[+-]?([0-9]+)$ ]]
then
echo "You introduced an integer value (without decimals)"
read
else
if [[ $var =~ ^[+-]?([0-9]+\.)$ ]]
then
echo "Incomplete floating value (no values provided at the right of the point)"
read
else
if [[ $var =~ ^[+-]?(\.[0-9]+)$ ]]
then
echo "Incomplete floating value (no values provided at the left of the point)"
read
else
if [[ $var =~ ^[+-]?([0-9]+\.[0-9]+)$ ]]
then
echo "You introduced a correct floating value"
read
else
echo "You introduced something other than a valid numeric value"
read
fi
fi
fi
fi
done

