문제

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

Thanks!

도움이 되었습니까?

해결책

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.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top