Search code examples
powershellescapingquoting

Regex pattern with embedded double quotes in PowerShell


Following keyword need to be searched on document using PowerShell:

["Allowed Acquisition Clean-Up Period" 
$keyword = ""
Get-Content $SourceFileName | Select-String -Pattern $keyword

String which I'm searching for has double quotes in it, so I'm struggling how to mention in this $keyword.


Solution

  • Apparently you not only have double quotes, but also an opening square bracket. Square brackets are meta-characters in regular expressions (for defining character classes), so you need to escape those too.

    Define your expression in single quotes:

    $keyword = '\["Allowed Acquisition Clean-Up Period"'
    

    or escape nested double quotes with backticks:

    $keyword = "\[`"Allowed Acquisition Clean-Up Period`""