Skip to content

Instantly share code, notes, and snippets.

@dstreefkerk
Created April 27, 2019 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstreefkerk/2d76505b4c8eb87daea66dd34d6245fb to your computer and use it in GitHub Desktop.
Save dstreefkerk/2d76505b4c8eb87daea66dd34d6245fb to your computer and use it in GitHub Desktop.
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