PowerShell: Regex Result ($Matches)
$Matches
-
$Matches
stores results of regular expression-match
or-replace
.$Matches
is a Hashtable$Matches
is a Automatic Variable
$Matches[0]
→ whole match string.$Matches[1]
→ first captured string.$Matches[2]
→ second captured string.$Matches[name]
→ named captured string.
🛑 WARNING: they store only the first occurrence.
"2024-12-14" -match "(\d{4})-" # True Write-Output $matches[1] # 2024 Write-Output $matches[0] # 2024-
$x = '<a href="major_maps.html">big maps</a>' # named capture $x -match 'href="(?<hrefval>[^"]+)"' Write-Host $matches["hrefval"] # major_maps.html
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