2

In RegExr.com, at the Examples section, there is a code for 2-5 letter palindromes. Using exactly the same pattern in Python doesn't seem to catch all results as RegExr!

Python doesn't catches words less than 4 characters, like 'dad' and '555'. The code is this

\b(\w)?(\w)\w?\2\1

What's the reason for these different results?

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
MKab
  • 23
  • 3
  • Take a look [here](http://stackoverflow.com/questions/23321575/regular-expression-for-finding-palindromes-behaving-strange) – Mauro Baraldi Apr 02 '16 at 12:38
  • Do you want to also include a small bit of Python code? i.e. are you using `re.match("\b(\w)?(\w)\w?\2\1")`? – Luke Taylor Apr 02 '16 at 12:39

1 Answers1

1

It works with a conditional in Python when the first capturing group is repeated:

\b(\w)?(\w)\w?\2(?(1)\1)

The (?(1)\1) translates to: if the first capturing group was successful, match the first captured group again.

The differences are most likely due to the way the regex engines treat an optional non-matched capturing group differently. E.g. RegExr uses the JavaScript engine in your browser, which treats an empty capturing group as undefined. When undefined is attempted to match at the end of the string again, it will succeed. In Python however, it's different and will only succeed to match again if the optional capturing group was successful. It's just down to implementation details.

James Buck
  • 1,640
  • 9
  • 13
  • 1
    Note that if you put the question marks *inside* the groups, the original regex will work in any flavor, as you can see [here](https://regex101.com/r/gR1dC1/1). That way, the groups can match nothing while still participating in the match. ECMAScript (the specification behind JavaScript and ActionScript, among others) is the only flavor I know of that treats a non-participating group the same as an empty group. – Alan Moore Apr 02 '16 at 13:51