Thread Problem im Zusammenhang mit REs: kniffelig (9 answers)
Opened by Crian at 2005-08-25 13:20

Crian
 2005-08-25 13:36
#57369 #57369
User since
2003-08-04
5870 Artikel
ModeratorIn
[Homepage]
user image
Ich hab das Lösungsprogramm nochmal etwas verfeinert:

Code: (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
#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper;

my $text = 'Vorrede bla bla Anfang <<A>> Teil1 <<A>> Teil2 <<B>> Teil3 <<A>> Ende Nachrede.';
my $retext = 'Anfang <<A>> [^<]+ (<<A>>) [^<]+ (<<B>>) [^<]+ <<A>> Ende';
print "Original:\n",
"text : [$text]\n",
"retext : [$retext]\n";

my $magicnumber = 0;
while ($text =~ s~(?<=\D)>>~#$magicnumber>>~) {
++$magicnumber;
}
$retext =~ s~>>~#\\d+>>~g;
my $re = qr~$retext~;
print "\nVeraendert:\n",
"text : [$text]\n",
"retext : [$retext]\n",
"re : [$re]\n";

my @allmatch = $text =~ $re;
my ($m1, $m2, $m3) = ($`, $&, $');

print "\nallmatch: ", Dumper \@allmatch;
print "m1 : [$m1]\n",
"m2 : [$m2]\n",
"m2 : [$m3]\n";


Ausgabe:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Original:
text : [Vorrede bla bla Anfang <<A>> Teil1 <<A>> Teil2 <<B>> Teil3 <<A>> Ende Nachrede.]
retext : [Anfang <<A>> [^<]+ (<<A>>) [^<]+ (<<B>>) [^<]+ <<A>> Ende]

Veraendert:
text : [Vorrede bla bla Anfang <<A#0>> Teil1 <<A#1>> Teil2 <<B#2>> Teil3 <<A#3>> Ende Nachrede.]
retext : [Anfang <<A#\d+>> [^<]+ (<<A#\d+>>) [^<]+ (<<B#\d+>>) [^<]+ <<A#\d+>> Ende]
re : [(?-xism:Anfang <<A#\d+>> [^<]+ (<<A#\d+>>) [^<]+ (<<B#\d+>>) [^<]+ <<A#\d+>> Ende)]

allmatch: $VAR1 = [
'<<A#1>>',
'<<B#2>>'
];
m1 : [Vorrede bla bla ]
m2 : [Anfang <<A#0>> Teil1 <<A#1>> Teil2 <<B#2>> Teil3 <<A#3>> Ende]
m2 : [ Nachrede.]
s--Pevna-;s.([a-z]).chr((ord($1)-84)%26+97).gee; s^([A-Z])^chr((ord($1)-52)%26+65)^gee;print;

use strict; use warnings; Link zu meiner Perlseite

View full thread Problem im Zusammenhang mit REs: kniffelig