Created
April 27, 2019 02:45
-
-
Save dstreefkerk/2d76505b4c8eb87daea66dd34d6245fb to your computer and use it in GitHub Desktop.
Some simple character replacement via Regex in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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