comparison - How do I match "|" in a regular expression in PowerShell? -
i want use regular expression filter out if string contains 1 of "&" or "|" or "=". tried:
$compareregex = [string]::join("|", @("&","|", "=")); "mydfa" -match $comparestr
powershell prints "true". not wanted, , seems "|" has confused powershell matching. how fix it?
@kayasax answer in case (thus +1), wanted suggest more general solution.
first of all: not using pattern you've created. suspect $comparestr
$null
, match anything.
to point: if want create pattern match characters/strings , can't predict if of them be/contain special character or not, use [regex]::escape()
item want match against:
$patternlist = "&","|", "=" | foreach-object { [regex]::escape($_) } $compareregex = $patternlist -join '|' "mydfa" -match $compareregex
in such case input can dynamic, , won't end pattern matches anything.
Comments
Post a Comment