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

Popular posts from this blog

java - Solr query version issue: Invalid version or the data in not in 'javabin' format -

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World