Your "fugly code" example suggests that you want to allow nesting of (cat ... ^cat) blocks, and that everything in the outer block should then get a single '#' inserted; and further, that if the final block is missing its closing "^cat" tag it should comment to the end of file. If both of those behaviours are required, then I'd probably prefer to stick with your example code - it makes it pretty clear what is going on, and could easily be extended to insert extra '#' characters for nested blocks if desired.
jo37's solution is nice, but does not handle nested blocks: it will stop commenting as soon as it sees the first "^cat".
If the "comment to end of file" behaviour is not desired, you may be better off with a recursive regexp that would only match balanced blocks.
In reply to Re: can u suggest a regex for this ?
by hv
in thread can u suggest a regex for this ?
by misterperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |