Regex question

Hello All,

I am not really good at regex. How do I filter out only those strings with ALL UPPERCASE (strictly no lowercase, numbers are allowed)?

In this below example, i would like to filter only “GB8639NKH6”

Here is what I tried.

PS [22:54:27] D:\> $A = "GB8639NKH6","86232a62","VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80"

PS [22:54:59] D:\> $a
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

PS [22:55:02] D:\> $A | where {$_ -cmatch "[A-Z]"}
GB8639NKH6
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

PS [22:55:33] D:\> $A | where {$_ -cmatch "[^a-z]"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

PS [22:55:57] D:\> $A | where {$_ -cmatch "[^a-z]*"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

PS [22:56:09] D:\> $A | where {$_ -cmatch "[^a-z]*$"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

PS [22:57:25] D:\> $A | where {$_ -cmatch "[A-Z]*$"}
GB8639NKH6
86232a62
VMware-56 4d 32 6a 30 ef 0b ff-59 ec fa 77 13 15 89 80

$a | where { $_ -cmatch '^[A-Z0-9]+$' }

^ indicates start of line
$ indicates end of line
[A-Z0-9] indicates a pattern of [A-Z or 0-9]

  • indicates the preceding pattern must match any number of characters from 1 to infinity
  • on the other hand indicates any number of characters from 0 to infinity, so using * would also match an empty string. + ensures there’s at least one character match.

And to match a specific number of characters, instead of + you should use

{num} to indicate a specific amount of characters that must be matched, or
{num1,num2} to indicate a range

So [1]{8}$ would match any string that is exactly 8 characters long and contain only A-Z or 0-9.


  1. A-Z0-9 ↩︎

Thanks Martin. I was really close :slight_smile: