April 15th, 2016

PowerTip: Use a regular expression pattern to remove nonalphabetic characters

Doctor Scripto
Scripter

Summary: Learn how to use a regular expression pattern to remove non-alphabetic characters from a string by using Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to remove non-alphabetic characters from a string?

Hey, Scripting Guy! Answer To remove nonalphabetic characters from a string, you can use the -Replace operator and substitute an empty string ‘’ for the nonalphabetic character. The secret to doing this is to create a pattern on characters that you want to include and then using the not (^) in the series symbol. Here is an example:

$string = ‘abcdefg12345HIJKLMNOP!@#$%qrs)(*&^TUVWXyz’

$pattern = ‘[^a-zA-Z]’

$string -replace $pattern, ‘ ‘

The Doctor

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.