user3657339
user3657339

Reputation: 627

Regex pattern for keyword with ( bracket

Following keyword need to be searched on document using PowerShell:

$keyword = "The Parent shall pay to the Security Agent ("
Get-Content $SourceFileName | Select-String -Pattern $keyword

$keyword has "(" opening bracket - how to mention this using regex in powershell

Upvotes: 0

Views: 163

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

By default Select-String does regular expression matches. In a regular expression parentheses have a special meaning: they group subexpressions and assertions. For matching a literal parenthesis in a regular expression it must be escaped with a backslash.

Generally speaking, if you want Select-String to match a pattern as a literal string either use the -SimpleMatch parameter:

Get-Content $SourceFileName | Select-String -Pattern $keyword -SimpleMatch

or escape the pattern:

Get-Content $SourceFileName | Select-String -Pattern ([regex]::Escape($keyword))

Upvotes: 2

Theo
Theo

Reputation: 61028

Escape the opening bracket with a backslash \

$keyword = "The Parent shall pay to the Security Agent \("

Upvotes: 0

Related Questions