On Mon, 14 Sep 2009, Spoon Reloaded wrote:

> In Perl I can match palindromes (strings that read the same forwards
> and backwards), and only palindromes, using the following pattern:
> /^(.?|(.)(?1)\2)$/

> However, this pattern does not work in PCRE. It doesn't seem to match
> any string longer than 1 character.

I now understand why this is different to Perl. In the PCRE 
documentation about recursion, it says:

  In  PCRE (like Python, but unlike Perl), a recursive subpattern call is
  always treated as an atomic group. That is, once it has matched some of
  the subject string, it is never re-entered, even if it contains untried
  alternatives and there is a subsequent matching failure.

This is the difference. The way you have written the pattern, it tries
for the single character first. If you write the alternatives the other 
way round, it works as you expect:

PCRE version 7.9 2009-04-11

/^((.)(?1)\2|.?)$/
    a
 0: a
 1: a
    aba
 0: aba
 1: aba
 2: a
    aabaa  
 0: aabaa
 1: aabaa
 2: a
    pqaabaaqp  
 0: pqaabaaqp
 1: pqaabaaqp
 2: p

I think I will use this example to make the point more clearly in the
documentation.

Philip

-- 
Philip Hazel

-- 
## List details at http://lists.exim.org/mailman/listinfo/pcre-dev 

Reply via email to