Question

I am trying to replace any spaces after > except for spaces after </a>. How can I do this using the Perl substitution operator?

Thanks!

Was it helpful?

Solution

To remove the spaces after > except </a>, you can use this:

$subject =~ s%(?<!</a)>\s*%>%mg;

How does it work?

  1. The % are the delimiters for the regex. They don't participate in the match.
  2. The (?<!</a) is a negative lookbehind that asserts "at this position in the string, what is behind me is not </a".
  3. 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.

OTHER TIPS

This should do it for you as it will only replace a space that follows a > and not a </a>

(?<=\>)(?<!\<\/a\>)[[:space:]]

REGEX101

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top