[英]Bash regex comparison issue
I have the following function 我有以下功能
checkFormat()
{
local funcUserName=$1
if [[ "$funcUserName" != [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9] ]];then
echo "1"
else
echo "0"
fi
}
if [[ $string != [a-zA-Z0-9]* ]]
Only returns true if the first character is not [a-zA-Z0-9] if [[ $string != [a-zA-Z0-9]{5} ]] 如果第一个字符不是[a-zA-Z0-9],如果[[$ string!= [a-zA-Z0-9] {5}]],则仅返回true
Never returns true. 从不返回真实。
if [[ $string != [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9] ]]
Returns as I want it to. 按我的意愿返回。 Why is this?
为什么是这样?
The code is to check that a username is 5 characters long and alphanumeric ie Joe12 or 12345 but not %$134. 代码是检查用户名是5个字符长和字母数字,即Joe12或12345,但不是%$ 134。
bash version 4.2.37 bash版本4.2.37
I suggest to replace 我建议更换
if [[ $string != [a-zA-Z0-9]{5} ]]
by 通过
if [[ ! $string =~ ^[a-zA-Z0-9]{5}*$ ]]
to match a regex. 匹配正则表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.