Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
regex-检查输入是否为csh(unix)中的数字_Regex_Unix_If Statement_Csh - Fatal编程技术网

regex-检查输入是否为csh(unix)中的数字

regex-检查输入是否为csh(unix)中的数字,regex,unix,if-statement,csh,Regex,Unix,If Statement,Csh,请帮忙。如何使正则表达式在if条件下工作, 检查用户输入(在第二个参数中)必须不等于任何数字 set regex="^[0-9]+$" if ($#argv == 2 && $2 != $regex) then # do this 基于grep的解决方案,仅适用于非负整数 #!/bin/csh # Note this requires 2 command line arguments, and checks the second one # cshell arguments

请帮忙。如何使正则表达式在if条件下工作, 检查用户输入(在第二个参数中)必须不等于任何数字

set regex="^[0-9]+$"
if ($#argv == 2 && $2 != $regex) then
# do this

基于grep的解决方案,仅适用于非负整数

#!/bin/csh

# Note this requires 2 command line arguments, and checks the second one
# cshell arguments are 0-indexed.
if ($#argv == 2) then

    # Note the different grep regexp syntax, and you must use single quotes
    set test = `echo $2 | grep '^[0-9]*$'`
    if (($test) || ($test == 0)) then
        echo "Bad input"
    else
        echo "Ok!"
    endif
endif

如果[$#argv==2&&!$2=~$regex]hi,我试过了,但现在收到了“变量语法”错误。
。&&&$2=~“$regex”]?(注意
regex
变量周围的dbl引号。对不起,我没有办法测试这个)。祝你们好运。你们正在尝试使用Bash语法。嗨,这很好用!但是,它不包括0。知道为什么吗?但是如果我把
^[-0-9]*$
放进去,它包括负数,但仍然不是0。这是现在唯一的问题,但谢谢!如果你知道解决方案,请帮助我,谢谢!它不会包含0,因为0在
if($test)
段中的计算结果为“False”。我所做的是在grep中添加了-c选项,并保留了其余的代码,它似乎正常工作。知道了-c是count,idk,但是你认为使用-c选项安全吗?像这样:set test=
echo$2 | grep-c'^[0-9]*$'
对我来说非常安全。请记住,我的解决方案和您的'-c'想法都不会匹配小数。为此,您需要一个更复杂的regexp。谢谢!!:)