To remove the spaces after >
except </a>
, you can use this:
$subject =~ s%(?<!</a)>\s*%>%mg;
How does it work?
- The
%
are the delimiters for the regex. They don't participate in the match. - The
(?<!</a)
is a negative lookbehind that asserts "at this position in the string, what is behind me is not</a
". - After asserting this, we match a
>
and a whitespace character.
Therefore, we have matched a >
string but not the wrong one.
The replacement, indicated by %>%
, is just a >
, allowing us to get rid of the whitespace character.