Reputation: 15
I am trying to replace any spaces after > except for spaces after </a>
. How can I do this using the Perl substitution operator?
Thanks!
Upvotes: 0
Views: 138
Reputation: 41838
To remove the spaces after >
except </a>
, you can use this:
$subject =~ s%(?<!</a)>\s*%>%mg;
How does it work?
%
are the delimiters for the regex. They don't participate in the match. (?<!</a)
is a negative lookbehind that asserts "at this position in the string, what is behind me is not </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.
Upvotes: 5
Reputation: 3175
This should do it for you as it will only replace a space that follows a >
and not a </a>
(?<=\>)(?<!\<\/a\>)[[:space:]]
PERL:
use strict;
use warnings;
while (<DATA>) {
$_ =~ s/(?<=\>)(?<!\<\/a\>)[[:space:]]/REPLACEMENT/gm;
print $_;
}
__DATA__
>
> Test
</a> TEST
<s m> TEST
Output:
>REPLACEMENT
>REPLACEMENTTest
</a> TEST
<s m>REPLACEMENTTEST
Upvotes: 1