Weekend Scripter: Exploring Palindrome Sentences with PowerShell


Summary: Ed Wilson, Microsoft Scripting Guy, talks about identifying palindromes that are more than a single word.

Microsoft Scripting Guy, Ed Wilson, is here. One of the coolest things about palindromes is that they can be formed with sentences. It is also what makes them the most subtle. For example, whereas it is pretty obvious that mom, dad, and the name bob are all the same forward and backward, it is not so obvious that the sentence “Never a foot too far, even” is also considered a palindrome. I mean, if I stand on my head, and hold my ear just so, maybe I get it. Clearly, this is a job for Windows PowerShell.

The script I wrote yesterday in Use PowerShell to Find Palindromes will identify a palindrome in a single word.

It will also do simple palindrome sentences such as noel sees leon. But if I try to do something more complicated, such as "No, it can assess an action," then it fails. This is shown in the following image:

Image of command output

I need to strip out all non-alphabetic characters, spaces, commas, periods, and the like, and replace them with no spacing at all. In this way, I am only comparing the letters that make up the palindrome sentence. This is the way a palindrome is evaluated anyway.

To do this, I will use the replace operator and specify a regex pattern that matches anything that is not a lower-case letter or an upper-case letter.

As it turns out...

It turns out that the change is incredibly easy to do. All I need to do is to use the '[^a-zA-Z]' regex pattern with the –Replace operator. I specify that the replacement will be ‘’ , which is nothing. I want to test it, so I create a string that contains a little punctuation and some spacing, and I then replace it with my pattern. As shown here, it works:

PS C:\> "A string, one that can't just contain letters!" -replace '[^a-zA-Z]',''

Astringonethatcantjustcontainletters 

Where to put it?

I now have a regex pattern that will work for me, but where do I add it to my script? At first I was thinking that I would need to loop around, and make replacements, but then the answer dawned on me. I can add it to my first variable assignment. I know the sentence, so when I add it to the script and I have my variable, I no longer need it. As shown here, I added it to the end of the string assignment:

$a = 'No, it can assess an action' -Replace '[^a-zA-Z]',''

Here is the complete script:

$a = 'No, it can assess an action' -Replace '[^a-zA-Z]',''

$b = $a.ToCharArray()

[array]::reverse($b)

$c = -join($b)

"$a equals $c`:`r`n-----"

$a -eq $c

The script and the output from the script are shown in the following image. The new code, which I added to yesterday’s script, is highlighted in yellow.

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (2)

  1. Chris J Warwick says:

    Almost all the examples I see of the -replace operator fail to point out that when replacing with ” (nothing) the second parameter is not required.

    So,

    ‘xyz’ -replace ‘x’,”

    Can be written as:

    ‘xyz’ -replace ‘x’

    (Also, as regex in PowerShell is case insensitive by default, your pattern can be simplified to [^a-z])
    (…Case insensitivity doesn’t by default apply to .Net methods where [^a-zA-Z] would be required.)

  2. ed wilson says:

    @Chris these are both great points. Thank you for sharing them.

Skip to main content