Leser: 21
my $pattern = qr/^(?<t>hund|katze):(?<n>[^,]),(?<g>\d+),(?<s>m|w)$/;
my $pattern = qr/^(?<t>hund|katze):(?<n>[^,])?(<t>=hund,(?<g>\d+)),(?<s>m|w)$/;
1 2 3 4 5 6 7 8 9
my ($kind, $info) = split /:/, ..., 2; my ($name, $weight, $gender); if ($kind eq 'hund') { ($name, $weight, $gender) = split /,/, $info, 3; } else { ($name, $gender) = split /,/, $info, 2; }
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
#!/usr/bin/perl use strict; use warnings; my $pattern = qr/^((?<k>katze)|(?<h>hund)):(?<n>[^,]+),(?(<h>)(?<g>\d+),)(?<x>w|m)/; blub("hund:waldi,23,m"); blub("katze:rosa,w"); sub blub { my $string = shift; print "-"x20; print "\nString:\t\t$string\n"; if ($string =~ $pattern) { print "Tier:\t\t"; if (defined($+{k})) { print "$+{k}\n"; } elsif (defined($+{h})) { print "$+{h}\n"; } print "Name:\t\t$+{n}\n" if (defined($+{n})); print "Gewicht:\t$+{g}kg\n" if (defined($+{g})); print "Geschlecht:\t$+{x}\n" if (defined($+{x})); } else { print "no matches...\n"; } }
1
2
3
4
5
6
7
8
9
10
11
12
$ ./test.pl
--------------------
String: hund:waldi,23,m
Tier: hund
Name: waldi
Gewicht: 23kg
Geschlecht: m
--------------------
String: katze:rosa,w
Tier: katze
Name: rosa
Geschlecht: w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
my $blub = "hund:bello,12,w";
my $blab = "katze:mini,m";
my $pattern = qr{
^(?:(?<t>hund)|(?<t>katze)) # hund oder katze, zusätzlich in \1 und \2 capturen
\: # trenner
(?<n>[^\,]+) # name
\, # trenner
(?(1)
(?<g>\d+) # gewicht nur wenn \1 gematcht hat (e.g. hund)
\, # trenner
)
(?<s>m|w) # geschlecht
$
}x;
1
2
3
4
5
6
7
8
9
10
11
$VAR1 = {
'n' => 'bello',
'g' => '12',
's' => 'w',
't' => 'hund'
};
$VAR1 = {
'n' => 'mini',
's' => 'm',
't' => 'katze'
};
Guest PHilbsHallo vifo,
danke für deine Antwort, ich glaube, ich habs nun gleich gelöst, wie du oder?
Gruß
PHilbs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @list=( 'hund:bello,12,w', 'katze:mini,m', 'hund:lessi,8,m', 'katze:kassandra,w', ); for my $line (@list) { $line=~/^(?<t>katze|hund):(?<n>[^,]+),(?:(?<g>\d+),)?(?<s>w|m)$/; $line={%+}; } print Dumper(\@list);
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/usr/bin/env perl # Core Modules use strict; use warnings; use 5.010; use utf8; use open ':encoding(UTF-8)'; use open ':std'; use Carp; use Try::Tiny; use Data::Dumper; { my $name = qr/(?<n> [^,]++ )/xms; my $weight = qr/(?<g> \d++ )/xms; my $sex = qr/(?<s> m|w )/xms; my %types = ( hund => qr/\A (?<t> hund ) : $name , $weight , $sex \z/xms, katze => qr/\A (?<t> katze ) : $name , $sex \z/xms, ); sub get_data { my ( $string ) = @_; # read type my $type; if ( $string =~ m/\A ([^:]++) : /xms ) { $type = $1; } else { croak "String [$string] is probably invalid"; } # get data from string if ( !exists $types{$type} ) { croak "Don't knew what to do with type [$type]."; } # return data $string =~ $types{$type}; return { %+ }; } } my @animals = ( "hund:bello,12,w", "katze:mini,m", "maus:Mickey,12,w", "muahahahaha", ); for my $animal ( @animals ) { try { my $data = get_data($animal); print Dumper($data); } catch { warn $_; }; }
1
2
3
4
5
6
7
8
9
10
11
12
13
$VAR1 = {
'n' => 'bello',
'g' => '12',
's' => 'w',
't' => 'hund'
};
$VAR1 = {
'n' => 'mini',
's' => 'm',
't' => 'katze'
};
Don't knew what to do with type [maus]. at ./match.pl line 60
String [muahahahaha] is probably invalid at ./match.pl line 60
maus => qr/\A (?<t> maus ) : $name , $weight, $sex \z/xms,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use strict; use warnings; use Data::Dumper; my $blub = "hund:bello,12,w"; my $blab = "katze:mini,m"; for ($blub, $blab) { if (/^ (?<t>hund) : (?<n>[^,]*) , (?<g>\d+) , (?<s>m|w) $ | ^ (?<t>katze) : (?<n>[^,]*) , (?<s>m|w) $ /x) { print Dumper \%+; } }
1
2
3
4
5
6
7
8
9
10
11
$VAR1 = {
'n' => 'bello',
'g' => '12',
's' => 'w',
't' => 'hund'
};
$VAR1 = {
'n' => 'mini',
's' => 'm',
't' => 'katze'
};