What I'd recommend instead is to tokenize your text, that is split it up into chunks that are 1) either normal text or 2) your special comments or 3) opening or closing <a> tags.
Then iterate over all these chunks, and count the difference in the number of opening and closing anchor tags. While iterating over these tokens you construct an output string, and in that string it shouldn't be too hard to get the nesting of <a> tags correctly.
In reply to Re: Regex within html
by moritz
in thread Regex within html
by ropey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |