Regular Expression

DrOverflow

Erfahrenes Mitglied
Hallo Leute!

Muss ein Projekt, welches in Perl geschrieben ist, überarbeiten, und bin dabei auf folgenden mysteriösen Ausdruck gestoßen:

Code:
my $pattern = '^([^\=]+)\=(.*)$';

Ich weiß zwar, wofür die REs gut sind und was sie bewirken - aber ich tu mir schwer beim Entschlüsseln.

Was bewirkt diese RE?! :confused:

Wär super, wenn mir jemand weiterhelfen könnte - danke!

lg D;-]c
 
Das sammelt alles bis zum ersten "=" und alles danach...

Code:
#!/usr/bin/perl

use strict;
use warnings;

my $pattern = '^([^\=]+)\=(.*)$';

my @texte = ('Key=Value', 'Hallo=Test', 'Dies=ist=ein=Test');
for(@texte){
    my ($key,$value) = $_ =~ /$pattern/;
    print "Key: $key -- Value: $value\n";
}

Code:
~/entwicklung 61> perl key_value.pl 
Key: Key -- Value: Value
Key: Hallo -- Value: Test
Key: Dies -- Value: ist=ein=Test


Aber um ehrlich zu sein, sollte man das besser mit split machen (schneller, speicherschonender, lesbarer)

Code:
my @texte = ('Key=Value', 'Hallo=Test', 'Dies=ist=ein=Test');
for(@texte){
    my ($key,$value) = split /=/,$_,2; # teile am ersten "="
    print "Key: $key -- Value: $value\n";
}

Wenn Du mal wieder eine RegEx erklärt haben möchtest und Du möchtest nicht erst auf Antwort warten, kannst Du auch das Modul YAPE::Regex::Explain verwenden...

Code:
The regular expression:

(?-imsx:^([^\=]+)\=(.*)$)

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 and capture to \1:
----------------------------------------------------------------------
    [^\=]+                   any character except: '\=' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \=                       '='
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
 
Zurück