Thread Regex Frage (23 answers)
Opened by bianca at 2012-11-09 17:42

bianca
 2012-11-10 09:01
#163233 #163233
User since
2009-09-13
6977 Artikel
BenutzerIn

user image
2012-11-09T19:04:23 GwenDragon
qr ist dein Freund.

Danke dir aber ich könnte heulen.
Was ist jetzt schon wieder falsch?
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/perl
use strict;
use warnings;

my $regex = qr{
    /^\[
    \S+\s+
    (\S+)\s+
    ([0-9]+)\s+([0-9:]+)\s+([0-9]+) # Datum
    \]
    \s+
    \[
    ([^\]]+) # Fehler
    \]
    \s+
    (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead 
    (.+)
    $
}x;

my @test;
push @test,'[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HTTP request';
push @test,'[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can\'t modify constant item in scalar assignment at /script.pl line 16, near "\'schiess mich tot\';", referer: https://.......pl';
for (my $z = 0; $z < scalar @test; $z++) {
    print "Zeile $z: ";
    if (
        $test[$z] =~ $regex
    ) {
        print "matcht 1='$1' 2='$2' 3='$3' 4='$4' 5='$5' 6='$6' 7='$7'\n";
    }
    else {
        print "matcht nicht\n";
    }
}

Nichts matcht. Warum ist das so?

Edit:
Habe es mal so versucht:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my $regex = qr{
    ^\[
    \S+\s+
    (\S+)\s+
    ([0-9]+)\s+([0-9:]+)\s+([0-9]+) # Datum
    \]
    \s+
    \[
    ([^\]]+) # Fehler
    \]
    \s+
    (?>\[\S+\s([^\]]+)\]\s+)? # falls [client ...] Lookahead 
    (.+)
    $
}sx;

das führt zu:
Quote
Use of uninitialized value $7 in concatenation (.) or string at test.pl line 29.
Zeile 0: matcht 1='[Thu Nov 08 22:59:19 2012] [error] Hostname www.......de provided via SNI, but no hostname provided in HT
TP request' 2='Nov' 3='08' 4='22:59:19' 5='2012' 6='error' 7=''
Zeile 1: matcht 1='[Fri Nov 09 09:49:30 2012] [error] [client 8.1.1.1] Can't modify constant item in scalar assignment at /s
cript.pl line 16, near "'schiess mich tot';", referer: https://.......pl/' 2='Nov' 3='09' 4='09:49:30' 5='2012' 6='error' 7='
8.1.1.1'

matcht zwar aber alles in $1. Aber ohne Modif. /s matcht garnichts. Wieso?
Der Regex Helper von Notepad matcht ebenfalls nicht. Auch einzeilig und ohne Kommentare im Regex. Bleibt 0.
Last edited: 2012-11-10 09:38:45 +0100 (CET)
10 print "Hallo"
20 goto 10

View full thread Regex Frage