Regular Expression For Finding Palindromes Behaving Strange
I want to write a program which finds palindromes (words which are the same from start to end and end to start like anna). But it should also work for multiple words car a rac and
Solution 1:
Your regular expression cannot match overlapping regions (you'd need to play with look-arounds with capturing groups to do that).
The expression matches the first three x characters first; it matches:
- one character (group 1), zero spaces (group 2), an optional character (the
?is greedy), the zero spaces from group 2, the one character from group 1.
The second match then has to start after that; the two xx characters match because the [a-z]? pattern is optional.
You cannot create a regular expression to match palindromes in general (at least not with the Python re engine), as there is no facility to match an arbitrary-width previous group in reverse.
Post a Comment for "Regular Expression For Finding Palindromes Behaving Strange"