in reply to quoting characters

After thinking about the problem some more, I realized that my solution would not handle another case, not represented by the examples in the original question, namely something as silly as:

This code stops here # but comments continue \# with a fake extension +here
A workaround is to make the regex into two expressions:
s/(\\*)(#[^#\\]*)/length($1)?length($1)%2?"$1$2":$1:"TURNSTEP"/eg; s/TURNSTEP.*//;

This makes sure that everything after a genuine comment is removed, period.

Ideally, you'd use something shorter and not likely to be in the input, e.g. a control character or something, but then again, 'TURNSTEP' is not very likely either. (If it is, I'd like to see that code! ;)