Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some simple character replacement via Regex in PowerShell
# Regex Examples with -Replace
$testString = "ABCabc 123456_!#$%"
Write-Host "Remove all numbers in a string" -ForegroundColor Yellow
"Before: $testString"
"After: $($testString -replace '\d')"
""
Write-Host "Remove everything but numbers from a string" -ForegroundColor Yellow
"Before: $testString"
"After: $($testString -replace '\D')"
""
Write-Host "Remove whitespace characters (spaces, tabs, etc) from a string" -ForegroundColor Yellow
"Before: $testString"
"After: $($testString -replace '\s')"
""
Write-Host "Remove specific characters from a string" -ForegroundColor Yellow
"Before: $testString"
"After: $($testString -replace '[!$]')"
""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.