PowerShell: Regular Expression Operators
String Match by Regex
-match
-
String matches regex pattern. Case-insensitive.
match result stored in $Matches
# check if contain 3 digits # the regex must be on right side "some_724.jpg" -match "\d{3}" # True
-iMatch
-
Case-insensitive. Same as
-Match
. -cMatch
-
Case-sensitive
-notMatch
-
Negation.
-iNotMatch
-
Case-insensitive. Same as
-Notmatch
. -cNotMatch
-
Case-sensitive
String Regex Replace
-Replace
-
Replace the matched part of a regex in string. Case-Insensitive.
match result stored in $Matches
# replace the regex match part of a string $output = "some 04/02/2025 yes" -replace "(\d{2})/(\d{2})/(\d{4})", '$3-$1-$2' Write-Host $output # some 2025-04-02 yes
-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 and regular expression
- PowerShell: String
- PowerShell: Single Quoted String
- PowerShell: Double Quoted String
- PowerShell: Here-String
- PowerShell: Escape Characters
- PowerShell: String Length
- PowerShell: Join String
- PowerShell: Split String
- PowerShell: Format String
- PowerShell: String Methods
- PowerShell: String Wildcards
- PowerShell: Regular Expression Operators
- PowerShell: Regex Result ($Matches)
- PowerShell: Regular Expression Syntax