Simple string match, what am I missing?

Welcome Forums General PowerShell Q&A Simple string match, what am I missing?

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 6 days ago.

  • Author
    Posts
  • #177598

    Participant
    Topics: 26
    Replies: 59
    Points: 96
    Rank: Member

    I have this in an if statement and today it errors and it never has before. I tried it on another prompt and same error.
    What am missing, why is it reading the \h as a escape h? I have tried it with single and double quotes.

    PS $SecureKeyPath = 'C:\01servers\hdi\'
    $SecureKeyPath -match "C:\01servers\hdi\"
    parsing "C:\01servers\hdi\" – Unrecognized escape sequence \h.
    At line:2 char:1
    + $SecureKeyPath -match "C:\01servers\hdi\"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

    PS $PSVersionTable

    Name Value
    —- —–
    PSVersion 5.1.18362.145
    PSEdition Desktop
    PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
    BuildVersion 10.0.18362.145
    CLRVersion 4.0.30319.42000
    WSManStackVersion 3.0
    PSRemotingProtocolVersion 2.3
    SerializationVersion 1.1.0.1

  • #177613

    Participant
    Topics: 39
    Replies: 158
    Points: 516
    Helping Hand
    Rank: Major Contributor

    You need to escape the \

    $SecureKeyPath = 'C:\01servers\hdi\'
    $SecureKeyPath -match "C:\\01servers\\hdi\\"

     

     

  • #177616

    Participant
    Topics: 39
    Replies: 158
    Points: 516
    Helping Hand
    Rank: Major Contributor

    You could also use

    $SecureKeyPath -match [Regex]::Escape('C:\01servers\hdi\')
  • #177619

    Participant
    Topics: 9
    Replies: 425
    Points: 689
    Helping Hand
    Rank: Major Contributor

    Per https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-6
    "-match Returns true when string matches regex"

    So you have to look at the 'C:\01servers\hdi\' as a Regular Expression (RegEx)

    In RegEx the \ is one of the +*?^$\.[]{}()|/ special characters

    To do the desired string comparison you can 'escape' the \ by adding another one before it like:

    $SecureKeyPath = 'C:\01servers\hdi\'
    $SecureKeyPath -match "C:\\01servers\\hdi\\"
    

    Note that -match will return TRUE if $SecureKeyPath includes the pattern "C:\\01servers\\hdi\\"
    So the following will also return TRUE

    'blablaC:\01servers\hdi\blabla' -match "C:\\01servers\\hdi\\"
    

    If you want to validate that $SecureKeyPath has the value 'C:\01servers\hdi\' exactly I would use the -eq operator (not case sensitive) instead of -match

    'C:\01servers\hdi\' -eq 'c:\01servers\hdi\'
    
  • #177655

    Participant
    Topics: 26
    Replies: 59
    Points: 96
    Rank: Member

    Yeah, I was thinking I could get away with an exact match if the match had single quotes. But remembering that it is a regex, reminds me that is silly. Thanks!

  • #177894

    Participant
    Topics: 2
    Replies: 496
    Points: 1,221
    Helping Hand
    Rank: Community Hero

    Yeah, PowerShell knows to interpret a string as literal, but to the regex classes, a string is a string; .NET doesn't really have the distinction of literal / expandable strings except as syntactic sugar, it's not a fundamental part of the underlying implementation as much as it is in parts of PowerShell's code.

    As mentioned above, the [regex]::Escape() method is a very handy shortcut to have for when you want a literal match. 🙂

     

You must be logged in to reply to this topic.