在 PowerShell 中检查字符串中的所有小写字母

发布于 2024-08-18 06:48:28 字数 227 浏览 6 评论 0原文

我希望能够测试 PowerShell 字符串是否全部为小写字母。

我不是世界上最好的正则表达式猴子,但我一直在尝试这样做:

if ($mystring -match "[a-z]^[A-Z]") {
    echo "its lower!"
}

但当然它们不起作用,并且搜索互联网也没有让我得到任何结果。有没有办法做到这一点(除了循环测试每个字符之外)?

I want to be able to test if a PowerShell string is all lowercase letters.

I am not the worlds best regex monkey, but I have been trying along these lines:

if ($mystring -match "[a-z]^[A-Z]") {
    echo "its lower!"
}

But of course they doesn't work, and searching the Internet hasn't got me anywhere. Is there a way to do this (besides testing every character in a loop)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满栀 2024-08-25 06:48:28

PowerShell 默认情况下匹配不区分大小写,因此您需要使用 -cmatch 运算符:

if ($mystring -cmatch "^[a-z]*$") { ... }

-cmatch 始终区分大小写,而 -cmatch 始终区分大小写,而 -cmatch 始终区分大小写。 >-imatch 始终不区分大小写。

旁注:你的正则表达式也有点奇怪。基本上你想要我在这里提供的一个,它由

  • anchor 用于字符串的开头 (^)
  • 字符类 小写拉丁字母 ([az])
  • A 量词,指示重复字符类至少 0 次,从而根据需要匹配尽可能多的字符 (*)。您可以使用 + 来禁止使用空字符串。
  • 字符串末尾的 锚点 ($)。这两个锚点确保正则表达式必须匹配字符串中的每个字符。如果您只使用 [az]* ,那么这将匹配任何至少包含 0 个小写字母某处的字符串在其中。这将是每个字符串。

PS:不过,艾哈迈德有一个观点,如果您的字符串可能还包含字母以外的其他内容,并且您想确保其中的每个字母都是小写的,而不是还要求字符串仅由字母组成,那么您必须反转字符类,排序如下:

if ($mystring -cmatch "^[^A-Z]*$") { ... }

字符类开头的 ^ 反转 类,匹配每个字符 不包括。因此,只有当字符串在某处包含大写字母时,该正则表达式才会失败。尽管如此,仍然需要-cmatch

PowerShell by default matches case-insensitively, so you need to use the -cmatch operator:

if ($mystring -cmatch "^[a-z]*$") { ... }

-cmatch is always case-sensitive, while -imatch is always case-insensitive.

Side note: Your regular expression was also a little weird. Basically you want the one I provided here which consists of

  • The anchor for the start of the string (^)
  • A character class of lower-case Latin letters ([a-z])
  • A quantifier, telling to repeat the character class at least 0 times, thereby matching as many characters as needed (*). You can use + instead to disallow an empty string.
  • The anchor for the end of the string ($). The two anchors make sure that the regular expression has to match every character in the string. If you'd just use [a-z]* then this would match any string that has a string of at least 0 lower-case letters somewhere in it. Which would be every string.

P.S.: Ahmad has a point, though, that if your string might consist of other things than letters too and you want to make sure that every letter in it is lower-case, instead of also requiring that the string consists solely of letters, then you have to invert the character class, sort of:

if ($mystring -cmatch "^[^A-Z]*$") { ... }

The ^ at the start of the character class inverts the class, matching every character not included. Thereby this regular expression would only fail if the string contains upper-case letters somewhere. Still, the -cmatch is still needed.

素罗衫 2024-08-25 06:48:28

如果您的测试如此简单,您可以而且可能应该避免使用正则表达式:

$mystring -ceq $mystring.ToLower()

If your test is so simple, you can and probably should avoid the use of regular expressions:

$mystring -ceq $mystring.ToLower()
蝶舞 2024-08-25 06:48:28

试试这个模式,它匹配任何大写字母:"^[^AZ]*$"

对于任何大写字母,这都会返回 false,同时允许字符串包含其他项目,只要所有字母均为小写即可。例如,“hello world 123”是有效的。

如果您严格想要没有空格、数字等的字母,那么约翰内斯的解决方案适合。

Try this pattern, which matches anything that is not an uppercase letter: "^[^A-Z]*$"

This would return false for any uppercase letters while allowing the string to contain other items as long as all letters are lowercase. For example, "hello world 123" would be valid.

If you strictly want letters without spaces, numbers etc., then Johannes's solution fits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文