locked
'()' Behaves differently to ' () ' RRS feed

  • Question

  • In powershell Version 5.1.14409.1012 when I do >'(A)' -match '(A)' I get True, as expected. However when I do anything that involves a space between the apostrophe and the parenthesis I get False which I would not expect.

    '(A)' -match '(A)' = True

    ' (A)' -match ' (A)' = False

    ' (A) ' -match ' (A) ' = False

    '(A)' -match '(A)' = False

    Is this expected behaviour? And if so how do I check something like 'blah (blahInsideBrackets) blah111' for similarity with 'blah (blahInsideBrackets) blah222'...?

    Wednesday, October 18, 2017 10:17 AM

Answers

  • The part before the "-match" is a literal string the part after the "-match" is a regex pattern!! If you're looking for a literal parenthesis you would have to escape them.
    Get-Help about_Regular_Expressions

    Best regards (79,108,97,102|%{[char]$_})-join''

    So it could be something like this:

    'blah (blahInsideBrackets) blah111' -match '.*\(blahInsideBrackets\).*'


    • Edited by BOfH-666 Wednesday, October 18, 2017 11:26 AM
    • Proposed as answer by jrv Wednesday, October 18, 2017 11:43 AM
    • Marked as answer by Richard MuellerMVP Saturday, October 28, 2017 1:37 PM
    Wednesday, October 18, 2017 11:24 AM