Perhaps the following, which uses a negative lookhead and a negative lookbehind, will be helpful:
use strict;
use warnings;
my $test = "this is not OK < but this is ok <url>";
print rep($test);
sub rep {
$_[0] or return $_[0];
$_[0] =~ s/<(?!url)|(?<!url)>//g;
return $_[0];
}
Output:
this is not OK but this is ok <url>
Your sub returns '' if $_[0] evaluates to false. This means that 0 and !defined $_[0] would convert to '', and perhaps that's your intent. In these cases, the above just returns what was sent (if anything), so you may need to adjust it.