仅识别大写和小写的字符串

时间:2016-04-29 18:08:21

标签: regex powershell

我想使用powershell来仅识别包含大写和小写字符的字符串。

$param = "Sam"

If ($param -cmatch "[A-Z]"){
    Write-Host "String has uppercase characters"
}

这就是我现在所拥有的,但只有在字符串中存在大写字符时才会返回。如果两者都存在于同一个字符串中,我希望它返回。

3 个答案:

答案 0 :(得分:1)

尝试

$param -cmatch "[A-Z]*.[a-z]" -or $param -cmatch "[a-z]*.[A-Z]"

您可以在http://regexstorm.net/tester

尝试不同的模式

(感谢briantist和Keith Thompson的更新模式。)

答案 1 :(得分:1)

我会用:

if ($param -cmatch '[a-z]' -and $param -cmatch '[A-Z]')

它必须满足两个匹配项,字符串中的某个小写字符和字符串中的单个大写字符。

答案 2 :(得分:0)

PowerShell支持

Lookaheads。所以你可以使用这个正则表达式

^(?=.*[A-Z])(?=.*[a-z]).*$

<强> Regex Demo

Powershell Code

If ($param -cmatch "^(?=.*[A-Z])(?=.*[a-z]).*$") { Write-Host "String has both upper and lowercase characters" }
相关问题