mbourgon
mbourgon

Reputation: 1336

PowerShell 3 regular expression - why is case insensitive not working?

I'm trying to figure out why the below code won't match when the case is different. The reason for the 3rd/4th line is to combine my log filters into one expression, while still allowing commas.

I've tried a bunch of different ways, but case still matters.

$combined_log = {"A", "B", "Error"}
$Log_Filters = "ERROR", "failed", "Note", "Warning"
#[regex]$Log_Filter_regex =  '(?i)^(' + (($Log_Filters|foreach{[regex]::escape($_) -replace ",","\,"}) –join "|") + ')$'
[regex]$Log_Filter_regex =  (($Log_Filters|foreach {[regex]::escape($_) -replace ",","\,"}) –join "|") 
$combined_log | where {$_ -imatch $Log_Filter_regex} 

Upvotes: 2

Views: 646

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627327

The -match / -imatch operators expect a string pattern to follow, so remove [regex] from the code line:

$Log_Filter_regex =  (($Log_Filters|foreach {[regex]::escape($_) -replace ",","\,"}) –join "|")

The pattern will look like ERROR|failed|Note|Warning and you will get the match.

enter image description here

Upvotes: 2

Related Questions