in reply to Complex regex and apparent failure of /g option

Your original string doesn't start out containing "[ELSIF" so, of course, the s/\[(?:IF|­ELSIF|ELSE­).../.../ges never replaces the "ELSIF" in the string that is not preceeded by "[".

It is only after the first iteration of the replacement where doing s/.../\[$1/sm ends up inserting the "[" before the "ELSIF" that makes the outer replacement capable of replacing it. But, by then, the outer replacement has already moved past the position in the string where it would make that replacement and so it won't do it unless you start it over.

See perldoc -q commas for a similar but simpler example. You can see that they also use an empty loop to deal with the problem. It also shows how look-aheads can be used to avoid the loop (though I'm not sure that approach will work here).

- tye