正则表达式 - 检查输入是否是csh中的数字(unix) [英] regex - to check if input is number in csh(unix)
本文介绍了正则表达式 - 检查输入是否是csh中的数字(unix)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮忙。如何使正则表达式在if条件下工作,
检查用户输入(在第二个参数中)必须不等于任何数字?
Please help. How to make regex work in an if condition, to check user input(in 2nd parameter) must be not equal to any number?
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
这篇关于正则表达式 - 检查输入是否是csh中的数字(unix)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文