Powershell判断是否包含大小写


支持所有PS版本

使用正则表达式可以检查一个字符中是否包涵一个大写字母:

$text1 = 'this is all lower-case'
$text2 = 'this is NOT all lower-case'

$text1 -cmatch '[A-Z]'
$text2 -cmatch '[A-Z]' 

结果将返回”true”或”false”

反过来检查是否包含小写,可以尝试这样:

$text1 = 'this is all lower-case'
$text2 = 'this is NOT all lower-case'

$text1 -cmatch '^[a-z\s-]*$'
$text2 -cmatch '^[A-Z\s-]*$' 

结果将返回”true”或”false”

总体来说,这次测试比较困难因为你需要考虑所有字符的合法性。在这个例子中,我采用了从a到z的小写字符串,空格和减号。

合法的字符串是嵌在“^”与“$”中间的(它表示行的开始和结尾)。星号代表量化前面任何合法字符串。

原文地址:Testing Whether Text Contains Upper Case

本文链接: https://www.pstips.net/testing-whether-text-contains-upper-case.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注