Palindrome RegEx

Just playing a bit with RegEx recursion and wrote this pattern to identify palindromes.

(.)((?R)?|.)(\\g1)

I’m sure it can be improved, but it’s a start.

What’s the best way to learn how to use RegEx…
I maaaay or may not be using too many split strings…

Are we not drawn onward, we few, drawn onward to new era?

I like this site:

http://www.regular-expressions.info

And, of course, I recommend that you get a good testing and development app like RegExRX. :slight_smile:

Naturally, post question here too so we can offer opinions on whether a regular expression is the right choice. (It isn’t always.)

Even better, this uses a subroutine so it can identify a solitary word or phrase. In other words, there would be no partial match with the string “aaba”.

(?(DEFINE)(?<sub>(?'letter'.)((?&sub)?|.)\\g{letter}))^(?&sub)$

Alwyn, that let me refine it further. This will match sentences like yours too:

(?x)	# FREE-SPACE MODE

# Begin define subroutines
(?(DEFINE)
(?<ignore>[[:punct:]\\x20\\t]*)
(?<sub>(?'letter'.)(?&ignore)((?&sub)?|.)(?&ignore)\\g{letter}(?&ignore))
)
# End define subroutines

^(?&ignore)(?&sub)$

That should ignore punctuation and spaces wherever it finds it, so palindrome sentences will match.

I had to use free-space mode since this was getting a bit hard to read.

All I can say Kem, is that your regex skills is way beyond mine.

Write a RegEx app and you’ll become an expert too.

Wait, don’t do that…

:slight_smile:

Here is the code for copy-and-paste:

dim rx as new RegEx
rx.SearchPattern = _
"(?x)	# FREE-SPACE MODE" + EndOfLine + _
"" + EndOfLine + _
"# Begin define subroutines" + EndOfLine + _
"(?(DEFINE)" + EndOfLine + _
"(?<ignore>[[:punct:]\\x20\\t]*)" + EndOfLine + _
"(?<sub>(?'letter'.)(?&ignore)((?&sub)?|.)(?&ignore)\\g{letter}(?&ignore))" + EndOfLine + _
")" + EndOfLine + _
"# End define subroutines" + EndOfLine + _
"" + EndOfLine + _
"^(?&ignore)(?&sub)$"

Ug, wish I could edit. Here is a correction that seems to work all the time:

(?x)	# FREE-SPACE MODE

# Begin define subroutines
(?(DEFINE)
(?<ignore>[[:punct:]\\x20\\t]*)
(?<sub>(?'letter'.)(?&ignore)((?&sub)|.)?(?&ignore)\\g{letter}(?&ignore))
)
# End define subroutines

^(?&ignore)(?&sub)$

And the code:

dim rx as new RegEx
rx.SearchPattern = _
"(?x)	# FREE-SPACE MODE" + EndOfLine + _
"" + EndOfLine + _
"# Begin define subroutines" + EndOfLine + _
"(?(DEFINE)" + EndOfLine + _
"(?<ignore>[[:punct:]\\x20\\t]*)" + EndOfLine + _
"(?<sub>(?'letter'.)(?&ignore)((?&sub)|.)?(?&ignore)\\g{letter}(?&ignore))" + EndOfLine + _
")" + EndOfLine + _
"# End define subroutines" + EndOfLine + _
"" + EndOfLine + _
"^(?&ignore)(?&sub)$"

Sickness. Absolute insanity. I’ve called for the guys with the stiff white coats :open_mouth:

Could you also specify the minimum length of the matched pattern in the RegEx???

Sort of, with a positive lookahead, but it would be imperfect since the whitespace would be counted after the initial “ignore” was matched. Better to strip the whitespace in code and check the length of the remaining characters.

But if you wanted to make sure it was at least five characters, for example…

(?x)	# FREE-SPACE MODE

# Begin define subroutines
(?(DEFINE)
(?<ignore>[[:punct:]\\x20\\t]*)
(?<sub>(?'letter'.)(?&ignore)((?&sub)|.)?(?&ignore)\\g{letter}(?&ignore))
)
# End define subroutines

^
(?&ignore)
(?=.{5,}) # Make sure there are at least 5 characters upcoming
(?&sub)
$