Search notes:

Perl regular expressions: negative lookahead

(?!PATTERN)
#!/usr/bin/perl
use warnings;
use strict;


print "Zero-width negative look-ahead assertion\n\n";

quote_ampersands("foo bar & baz"); # Replace this one
quote_ampersands("10 < 20"   ); # Don't replace this one
quote_ampersands("et&"      ); # Don't replace this one
quote_ampersands("4 > 2"     ); # Don't replace this one
quote_ampersands("abc&gtef"     ); # Replace this one

sub quote_ampersands {
  my $text = shift;

  printf "  %-20s", $text;

  $text =~ s/&(?!amp;|lt;|gt;)/&/g;
  
  printf "%-20s\n", $text;
}
Github repository about-perl, path: /regular-expressions/negative-lookahead.pl

See also

Perl regular expressions
Lookaround assertions
Vim: negative lookahead regular expression

Links

Perl's extended regular expressions

Index