Powershell Regex cheat sheet
A collection of regex’s that I always find myself looking up.
#Match while tagging match groups
'CowColour Brown' -match '(?<Attribute>\w+) (?<Value>\w+)' | out-null
$matches.Attribute
$matches.Value
#cowColour
#Brown
#Matching groups - your $matches object will have properties containing the valid matches
"Subnet:10.1.1.0/24" -match 'Subnet:(?<SiteSubnet>(?:\d{1,3}\.){3}\d{1,3}/\d+)'
#Replace to reformat a string
'This is a wild test' -replace '.*(w[^ ]+).*','Not so $1'
#Not so wild
#Lazy matching (to prevent over-matching) use a ? after the + or *
"<h1>MyHeading</h1>" -replace '<([^/]+?)>','<cow>' -replace '</([^/]+?)>','</cow>'
#<cow>MyHeading</cow>
#negative lookbehind
($DistinguishedName -split '(?<!\\),')[1..100] -join ','
Last Updated September 13