PowerTip: Use a regular expression pattern to remove nonalphabetic characters

The Scripting Guys

Dr Scripto

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

The Scripting Guys
Dr Scripto

Follow Dr Scripto   

No Comments.