in reply to spaces removed in backreference

There is probably an easy way to do this with a real parser (and it's not easy to imagine a situation where it makes sense to worry about spaces between html tags). But putting all that aside, I wonder why you wouldn't just do something like this, using look-behind and look-ahead assertions:
s/(?<=>) +(?=<)//g;
Or like this, using captures (possibly a little bit "less efficient", but not enough to worry about it):
s/(>) +(<)/$1$2/g;
(No need to "loop until done" -- the "g" modifier takes care of the whole string.)

I also wonder why you use backslashes instead of dollar signs for your captures in the replacement part. If you had use warnings; in your code, you would have been told: "\1 better written as $1..." (and I suspect there's a good reason why, but it escapes me at the moment).

Replies are listed 'Best First'.
Re^2: spaces removed in backreference
by Allasso (Monk) on Apr 25, 2010 at 09:49 UTC
    s/(?<=>) +(?=<)//g; s/(>) +(<)/$1$2/g;

    Don't know why I never thought of these...

    Thanks for the tip on use warnings