在PowerSell中,我如何在下面的代码中包含必要的代码,这些代码将拒绝任何完全由数字/数字组成的输入,但会接受带或不带连字符的字母/字母数字值,但不接受空格或句点( 。)?
DO {
$NewID = Read-Host -Prompt " NEW ID NAME of object (8-15 chars) "
} UNTIL ($NewID.Length -gt 7 -and $NewName.Length -lt 16)
答案 0 :(得分:1)
if($foo -notmatch '^\d+$' -and $foo -match '^\w[\w-]*$') { ...ok } else { ...bad }
示例:
@( "123", "123 T", "123T", "123-T-456", "123 T 456" ) |
foreach-object {
if( $_ -notmatch '^\d+$' -and $_ -match '^\w[\w-]*$') { "'$_' passed" }
else { "'$_' failed" }
}
结果:
'123' failed
'123 T' failed
'123T' passed
'123-T-456' passed
'123 T 456' failed