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

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -