Hey there !
I am always looking for reflexion subjects about regex. I would like a regex that matches every words that contain 2^n characters in a list of words (where n is a natural number).
To make it simple, let's say a word is just a sequence of o
Let's also say the list is composed by words followed by the number of characters they contain and separated by spaces
Of course you can't use these numbers, it is for reading purpose !
For exemple in the list :
o (1) ooo (3) oooooo (6) oooo (4) ooooooooo (9) oo (2) oooooooooooo (12) oooooooo (8)
We should have the following matches :
matches : 'o', 'oo', 'oooo', 'oooooooo'
Your regex must however respect some rules :
- You cannot use recursion
- You cannot use any feature specific to a language (or a few languages)
If you manage to find one (or a trick) that works in javascript, it would be awesome (I don't think this is possible, though) !
Of course, it doesn't need to work with javascript.
Solving the problem is not the point here, I am only interested in how to solve it !
Edit :
Sadly, nobody found anything I was looking for. The question is still opened to answers, there must be good ones !
By the way, here is what I came up with, even if there should be better than that :
\b(?:o|(?:(?(1)\1|o)(?=((?(1)\1\1|o))))+\1)\b
Demo here