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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758200928, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/regular-expressions/negative-lookahead(73): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78