I’m looking for an algorithm that can check if nested regex repeats are reducible. Assume that parsing the regex is already done.
Example:
(1{1,2}){1,2} === 1{1,4}
It matches 1, 11, 111, 1111 which can be rewritten as a single repeat
(1{2,2}){1,2} can not be reduced
It matches 11 and 1111 which can not be rewritten as a single repeat.
(1{10,11}){1,2} can not be reduced
(1{10,19}){1,2} === 1{10,38}
(1{1,2}){10,11} === 1{10,22}
(1{10,11})* can not be reduced
(1*){10,11} === 1*
I’ve been trying to find a pattern to this type of operation without having to match all possible solutions and look for holes that would prevent it from being reduced. There must be a simple function (f( A, B, C, D ) -> ( E, F )) that could solve an arbitrary input like this:
(1{A,B}){C,D} -> 1{E,F}
x{A,B}is unlimited, it can be repeated any number of times.(x{A,B}){C}is always reducible.A*(C+1) <= B*C + 1, you can reduce it, since there are no gap between the longest sequence ofCrepetitions, and the shortest sequence ofC+1repetitions.B = -1orD == -1means unlimited, likex*orx{5,}.Test cases: