Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:24:29+00:00 2026-06-08T13:24:29+00:00

This regular expression matches palindromes: ^((.)(?1)\2|.?)$ Can’t wrap my head around how it works.

  • 0

This regular expression matches palindromes:
^((.)(?1)\2|.?)$

Can’t wrap my head around how it works.
When does the recursion end, and when regex breaks from the recursive subpattern and goes to "|.?" part?

edit: sorry I didn’t explain \2 and (?1)

(?1) – refers to first subpattern (to itself)

\2 – back-reference to a match of second subpattern, which is (.)

Above example written in PHP. Matches both "abba" (no mid palindrome character) and "abcba" – has a middle, non-reflected character.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:24:31+00:00Added an answer on June 8, 2026 at 1:24 pm

    ^((.)(?1)\2|.?)$

    The ^ and $ asserts the beginning and the end of the string respectively. Let us look at the content in between, which is more interesting:

    ((.)(?1)\2|.?)
    1------------1 // Capturing group 1
     2-2           // Capturing group 2
    

    Look at the first part (.)(?1)\2, we can see that it will try to match any character, and that same character at the end (back reference \2, which refers to the character matched by (.)). In the middle, it will recursively match for the whole capturing group 1. Note that there is an implicit assertion (caused by (.) matching one character at the beginning and \2 matching the same character at the end) that requires the string to be at least 2 characters. The purpose of the first part is chopping the identical ends of the string, recursively.

    Look at second part .?, we can see that it will match one or 0 character. This will only be matched if the string initially has length 0 or 1, or after the leftover from the recursive match is 0 or 1 character. The purpose of the second part is to match the empty string or the single lonely character after the string is chopped from both ends.

    The recursive matching works:

    • The whole string must be palindrome to pass, asserted by ^ and $. We cannot start matching from any random position.
    • If the string is <= 1 character, it passes.
    • If the string is > 2 characters, whether it is accepted is decided by the first part only. And it will be chopped by 2 ends if matches.
    • The leftover if matches, can only be chopped by the 2 ends, or passes if its length is <= 1.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that matches this regular expression: ^.+:[0-9]+(\.[0-9]+)*/[0-9]+$ which can easily be
I have this Regular Expression that matches the following strings: <!-- 09-02-2009 ---> <!--
How can I update this regular expression to find and replace not just /news/
I want to use this regular expression in Python: <(?:[^]*[']*|'[^']*'[']*|[^'>])+> (from RegEx match open
I want to match D11-RONPLAYER_DEF_15_PO using this regular expression: D\[0-9]+-\[A-Z]*PLAYER_(DEF\[0-9]*)_(\[^_]+)_ but it does not
Im trying to find a regular expression that matches this kind of URL: http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html
I have the following Regular Expression which matches an email address format: ^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$ This
Wondering if someone can tell me why this regular expression doesn't work. Expression ->
Can you explain me what is the meaning of this regular expression. What would
How can I get a regular expression that matches any string that has two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.