2

I am trying to understand a regex in Java that finds if a string of ANY length is a palindrome or not, as below:

\A(?:(.)(?=.*?(\1\2?)\z))*?.?\2\z

It matches a palindrome of all lengths. Can someone explain how is it exactly working? Specifically this part:

(?=.*?(\1\2?)\z))

wherein the second capturing group(\1\2?) is back-referenced within itself(\2)?

ashwani kumar
  • 107
  • 1
  • 9

0 Answers0