Regex question

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of GJ GJ 1 year, 9 months ago.

  • Author
    Posts
  • #22734
    Profile photo of GJ
    GJ
    Participant

    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
    
    
  • #22742
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant
    $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.

  • #22743
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    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 ^[A-Z0-9]{8}$ would match any string that is exactly 8 characters long and contain only A-Z or 0-9.

  • #22745
    Profile photo of GJ
    GJ
    Participant

    Thanks Martin. I was really close 🙂

You must be logged in to reply to this topic.