I'm working on the MIU system problem from "Gödel, Escher, Bach" chapter 2.
One of the rules states
Rule III: If III occurs in one of the strings in your collection, you may make a new string with U in place of III.
Which means that the string MIII
can become MU
, but for other, longer strings there may be multiple possibilities [matches in brackets]:
MIIII
could yieldM[III]I
>>MUI
MI[III]
>>MIU
MUIIIUIIIU
could yieldMU[III]UIIIU
>>MUUUIIIU
MUIIIU[III]U
>>MUIIIUUU
MUIIIIU
could yieldMU[III]IU
>>MUUIU
MUI[III]U
>>MUIUU
Clearly regular expressions such as /(.*)III(.*)/
are helpful, but I can't seem to get them to generate every possible match, just the first one it happens to find.
Is there a way to generate every possible match?
(Note, I can think of ways to do this entirely manually, but I am hoping there is a better way using the built in tools, regex or otherwise)
(Edited to clarify overlapping needs.)