Given an input string with an arbitrary number of 'x' characters (x
, xx
, xxxxx
, xxxxxxxxxxxxx
and so on), how can one write a regex that matches the input string only if it has a prime number of 'x' characters? A string of length 1 should not be matched.
For example:
Match these:
xx
xxx
xxxxx
xxxxxxx
But not these:
x
xxxx
xxxxxxxxx
This is one solution I found - ^(?!(xx+)\1+$)
(here, as an answer to this problem). However, I would like to know why it works. Please share any alternate solutions as well.
I'm using the PCRE engine.
I realize that one would typically not use regexes for this sort of thing. I am simply curious about how it could be done.