Thread Regex negieren (27 answers)
Opened by fs at 2010-03-19 09:57

betterworld
 2010-03-19 14:31
#135074 #135074
User since
2003-08-21
2614 Artikel
ModeratorIn

user image
2010-03-19T09:46:50 fs
Ich bekomme als Benutzereingabe einen Pattern.


Man sollte Benutzereingaben nicht einfach als Regex ausführen. Vor allem, wenn es keine vertrauenswürdigen Benutzereingaben sind.

Zum Thema:
Code (perl): (dl )
1
2
3
4
5
my @strings = qw(blah blubb hallo welt hallohallohallo hallihallo);

my $re = qr{^(?!hallo\z)};

print "$_\n" for grep /$re/, @strings;


Gibt alle Strings ausser "hallo" aus.

Edit: Und das folgende gibt alle Strings aus, in denen nicht "hallo" vorkommt:
Code (perl): (dl )
1
2
3
4
5
my @strings = qw(blah blubb hallo welt hallohallohallo hallihallo);

my $re = qr{^(?:(?!hallo).)*\z}s;

print "$_\n" for grep /$re/, @strings;

Last edited: 2010-03-19 14:43:36 +0100 (CET)

View full thread Regex negieren