I have a PowerShell 5.1 script that reads one csv file, looks up the read value in another csv file (which has some characters in it like Ω and ±) and finally writes a result to a third csv file. The lookup csv comes from China as an Excel file and I convert it to csv utf8 from Excel.
It all works fine except my regex searches while they work great at regex101.com and on the command line they don't seem to work in the Where-Object
cmdlet where I need them.
So these work great. Notice Ω and ±1
PS C:\Users\grefgarg> $u = "14.3kΩ ±1% 0.1W ±100ppm/? 0603 Chip Resistor - Surface Mount RoHS"
PS C:\Users\grefgarg> $u -match "(^14.3kΩ | 14.3KOhm )"
PS C:\Users\grefgarg> $true
But this does not. Where $b
is the lookup csv and $c
is the column to search say "Description"
$b= import-csv $bfile -Encoding 'utf8'
$r = "(^14.3kΩ | 14.3KOhm )"
$a= $b | where-object {($_.$($c) -match $r )}
$a.Count is 0 If, however, I replace the Ω with \D it works again.
r$ = "(^14.3k\D | 14.3KOhm )"
I would like to use the Ω and ±1 in my regex but the \D works for now. I am asking to get a better understanding of how the pipeline, regex and encoding work.
I did try:
$PSDefaultParameterValues = @{ '*:Encoding' = 'utf8' }
I also searched for this specific issue of command line vs. Where-Object
but I didn't see anything.
Thanks, Gregory