在 If 条件下验证字符串不是 empty/null
Verifying a string is not empty/null in an If condition
我正在编写一个脚本,它将通过 Read-Host
(设置为 $String
)接受用户输入,我想避免因变量。因为我会经常使用它,所以我想将它实现到一个函数中,以验证没有使用无效字符。
我想我可以使用带有 ![string]::IsNullOrEmpty($String)
作为条件之一的 if 语句:
Function Test-ValidCharacters ($String, $ValidCharacters) {
if (($String -match $ValidCharacters) -and (!([string]::IsNullOrEmpty($String)))) {
return $true
}
else {return $false}
}
我也试过这个:
Function Test-ValidCharacters ($String, $ValidCharacters) {
if (($String -match $ValidCharacters) -and ($String -ceq "")) {
return $true
}
else {return $false}
}
在这两种情况下,当出现 $String 的读取主机提示时,我只需按回车键,脚本的行为就好像函数返回了 $True
(然后会遇到致命错误)。另一半工作 - 如果我包含 $ValidCharacters
函数 returns $False
未指定的字符,如预期的那样。
我确定我在这里遗漏了一些东西。我什至尝试做第二个嵌套的 if 语句并得到相同的结果。
编辑:这是我调用函数并注意到问题的代码片段。
$ValidCharacters = '[^a-zA-Z0-9]'
$FirstN = Read-Host -Prompt "New user's first name"
While (Test-ValidCharacters $FirstN $ValidCharacters -eq $false) {
Write-Output "$FirstN contains illegal characters. A-Z, a-z, and 0-9 are accepted."
$FirstN = Read-Host -Prompt "New user's first name"
}
假设 $ValidCharacters
本身不是空字符串并且包含一个 锚定的 字符范围正则表达式(regular expression) that covers the entire input string, such as ^[a-z0-9./:]+$
, given that the -match
operator 匹配任何 子字符串默认情况下 (请注意,参数的更好名称因此类似于 $ValidationRegex
):[1]
在第一个函数定义中,-and
操作的 RHS 是多余的 - 它不会向条件添加任何内容,因为如果 $String -match $ValidCharacters
是 $true
,根据定义,! [string]::IsNullOrEmpty($String)
也是如此。
相反,在第二个函数定义中你的-and
操作总是returns$false
,因为$String -ceq ""
根据定义 $false
,如果 LHS 返回 $true
.
假设您的意图是防止空输入或全空白输入,并确保任何字符串 - 去除偶然的前导 and/or 尾随空白 - 仅由预期字符组成,请使用以下内容:
Function Test-ValidCharacters ($String, $ValidCharacters) {
# Note: no strict need for `return`.
$String.Trim() -match $ValidCharacters
}
[1] 或者,坚持使用 $ValidCharacters
并传递仅描述 单个 有效字符的正则表达式,例如 '[a-z0-9./:]'
, 并在函数内部构造整个字符串匹配正则表达式 '^' + $ValidCharacters + '+$'
我正在编写一个脚本,它将通过 Read-Host
(设置为 $String
)接受用户输入,我想避免因变量。因为我会经常使用它,所以我想将它实现到一个函数中,以验证没有使用无效字符。
我想我可以使用带有 ![string]::IsNullOrEmpty($String)
作为条件之一的 if 语句:
Function Test-ValidCharacters ($String, $ValidCharacters) {
if (($String -match $ValidCharacters) -and (!([string]::IsNullOrEmpty($String)))) {
return $true
}
else {return $false}
}
我也试过这个:
Function Test-ValidCharacters ($String, $ValidCharacters) {
if (($String -match $ValidCharacters) -and ($String -ceq "")) {
return $true
}
else {return $false}
}
在这两种情况下,当出现 $String 的读取主机提示时,我只需按回车键,脚本的行为就好像函数返回了 $True
(然后会遇到致命错误)。另一半工作 - 如果我包含 $ValidCharacters
函数 returns $False
未指定的字符,如预期的那样。
我确定我在这里遗漏了一些东西。我什至尝试做第二个嵌套的 if 语句并得到相同的结果。
编辑:这是我调用函数并注意到问题的代码片段。
$ValidCharacters = '[^a-zA-Z0-9]'
$FirstN = Read-Host -Prompt "New user's first name"
While (Test-ValidCharacters $FirstN $ValidCharacters -eq $false) {
Write-Output "$FirstN contains illegal characters. A-Z, a-z, and 0-9 are accepted."
$FirstN = Read-Host -Prompt "New user's first name"
}
假设 $ValidCharacters
本身不是空字符串并且包含一个 锚定的 字符范围正则表达式(regular expression) that covers the entire input string, such as ^[a-z0-9./:]+$
, given that the -match
operator 匹配任何 子字符串默认情况下 (请注意,参数的更好名称因此类似于 $ValidationRegex
):[1]
在第一个函数定义中,
-and
操作的 RHS 是多余的 - 它不会向条件添加任何内容,因为如果$String -match $ValidCharacters
是$true
,根据定义,! [string]::IsNullOrEmpty($String)
也是如此。相反,在第二个函数定义中你的
-and
操作总是returns$false
,因为$String -ceq ""
根据定义$false
,如果 LHS 返回$true
.
假设您的意图是防止空输入或全空白输入,并确保任何字符串 - 去除偶然的前导 and/or 尾随空白 - 仅由预期字符组成,请使用以下内容:
Function Test-ValidCharacters ($String, $ValidCharacters) {
# Note: no strict need for `return`.
$String.Trim() -match $ValidCharacters
}
[1] 或者,坚持使用 $ValidCharacters
并传递仅描述 单个 有效字符的正则表达式,例如 '[a-z0-9./:]'
, 并在函数内部构造整个字符串匹配正则表达式 '^' + $ValidCharacters + '+$'