PowerShell: Regular Expression Operators

By Xah Lee. Date: .

String Match by Regex

-match
String matches regex pattern. Case-insensitive
"mycat.jpg" -match "\.+jpg"
# the regex must be on right side
-iMatch
Case-insensitive. Same as -Match.
-cMatch
Case-sensitive
-notMatch
Negation.
-iNotMatch
Case-insensitive. Same as -Notmatch.
-cNotMatch
Case-sensitive

String Regex Replace

-Replace
Replaces strings matching a regex pattern. Case-Insensitive.
"wonderland" -replace "wonder", "lala"
# result
# lalaland
-iReplace
Case-insensitive. Same as -Replace.
-cReplace
Case-sensitive

Escape Regex Meta Characters

[Regex]::Escape()
escape regex meta characters. Useful when a command or operator that takes regex, but you need plain text search.

PowerShell String