[perl] RegEx Muster /xxxx/xxxx/xxx/Inhalt/xxx

FlockY

Mitglied
Hi Community,

ich hab schon wieder eine Frage.

Ich habe ein Protokoll dass ich auswerten möchte ob nach dem vierten Slash ein A oder N steht.

also XXX/XXXXXXXXX/XXXXXXX/XXXX/A/XXXX/XXXX

Das problem ist, die Anzahl der Zeichen zwischen den Slashes variiert.

Ich bekomm das einfach nicht gebacken.

Vielleicht kennt jemand noch zusätzlich ein gut erklärendes Tutorial für die RegExpressions? Bei vielen versteh ich nur Bahnhof teilweise.


Bin für jeden Ratschlag dankbar.

Flocky.
 
Hiermit geht's:
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $string = 'XXX/XXXXXXXXX/XXXXXXX/XXXX/A/XXXX/XXXX';
if ( $string =~ m{^(?:[^/]+/){4}[AN]/} ) {
    print "yes\n";
}

Mehr zu m{} (oder auch m//) findest Du unter http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators

Noch mehr zu Regexen findest Du unter:
* http://www.regenechsen.de/phpwcms/index.php?regex
* http://p3rl.org/perlre
* http://p3rl.org/perlrequick
* http://p3rl.org/perlretut

Wenn Du Dir erklären lassen möchtest, was eine bestimmte Regex macht, dann kannst Du Dir das mit YAPE::Regex::Explain machen (gibt's auf CPAN - http://search.cpan.org).

Für den Regulären Ausdruck oben gibt's folgende Erklärung:

Code:
The regular expression:

(?-imsx:^(?:[^/]+/){4}[AN]/)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?:                      group, but do not capture (4 times):
----------------------------------------------------------------------
    [^/]+                    any character except: '/' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    /                        '/'
----------------------------------------------------------------------
  ){4}                     end of grouping
----------------------------------------------------------------------
  [AN]                     any character of: 'A', 'N'
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
 
Code:
^([^/]+/){4}([AN])(/[^/]+)?

Über Backreferences solltest Du auf die 2. Klammer zurückgreifen können
 
Das geht auch mit split:

Code:
my $fith_field = (split(qr|/|, "XXX/XXXXXXXXX/XXXXXXX/XXXX/A/XXXX/XXXX"))[4];
if ($fith_field eq "A" ) {
	print "true\n";
};
 
Zurück