I don't know the Text::Balanced module, so I can't tell you if there is a shortcut somewhere. But I can tell you why your regexes only work once.
First I would suggest leaving out all the unnecessary escapes. This makes your regex much more readable and less toothpicky ... $text =~ s/<!--(.*?)}(.*?)-->/<!--$1 endbrace $2-->/gxs;This matches a whole comment and within the comment the first closing brace is matched and replaced. Due to the /g modifier the regex continues searching, but not at the same starting character but the next one. So the same comment cannot match twice which is required to match multiple closing braces. The same goes for multiple opening braces.
You could fix that behaviour with the following construct 1 while s///; This repeats the whole substitution until no more substitution could be performed. It does the trick, but can be quite inefficient on large files as the whole text has to be search multiple times. Imagine e.g. a last line like this || }}}}}}}}}}
That's why I would go a different way for dealing with the comments - line by line simplifies things
The backsubstitution works similarly if you work on lines againforeach (@lines) { chomp; if (/^\s*\|\|/) { # is a comment s/}/endbrace/g; s/{/openbrace/g; s/^\s*||/<!--/; $_ .= "-->"; } }
Remark: I hope you don't have to deal with comment lines like this || min-->max++; because then things get tricky ...
-- Hofmator
In reply to Re: Problem with skipping comment
by Hofmator
in thread Problem with skipping comment
by tshabet
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |