Leser: 24
Quotezu zerlegen. Dazu habe ich ein Vorlage aus der Hilfe angepasst die auch für ein Argument klappt.\Befehl{arg1}{arg2}...{argn}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$re = qr /(?:[\s\n]*|[\s]*%[^\n]*\n)*
(?<first> # start capture buffer 1?<first>
\{ # match an opening paren
( # capture buffer 2
(?: # match one of:
(?> # don't backtrack over the inside of this group
[^{}]+ # one or more
) # end non backtracking group
| # ... or ...
(?&first) # recurse to opening 1 and try it again &first
)*? # 0 or more times.
) # end of buffer 2
\} # match a closing paren
) # end capture buffer one
/x;
$s =~ /\n[^%]*\\Befehl$re/g;
$s =~ /\n[^%]*\\Befehl(?:$re){1,9}/g;
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
$re = qr /(?:[\s\n]*|[\s]*%[^\n]*\n)*
(?<first> # start capture buffer 1?<first>
\{ # match an opening paren
( # capture buffer 2
(?: # match one of:
(?> # don't backtrack over the inside of this group
[^{}]+ # one or more
) # end non backtracking group
| # ... or ...
(?&first) # recurse to opening 1 and try it again &first
)*? # 0 or more times.
) # end of buffer 2
\} # match a closing paren
) # end capture buffer one
/x;
$s="\\Befehl{1}{2}{3}";
print "Test mit \$re\$re\$re:\n";
if ($s =~ /\\Befehl$re$re$re/g){
foreach $expr (1..$#-) {
print "\targument $expr = $$expr\n";
};
};
$s="\\Befehl{1}{2}{3}";
print "Test mit \$re{3}\n";
if ($s =~ /\\Befehl$re{3}/g){
foreach $expr (1..$#-) {
print "\targument $expr = $$expr\n";
};
};
$s="\\Befehl{1}{2}{3}";
print "Test mit (?:\$re){3}\n";
if ($s =~ /\\Befehl(?:$re){3}/g){
foreach $expr (1..$#-) {
print "\targument $expr = $$expr\n";
};
};
QuoteTest mit $re$re$re:
argument 1 = {1}
argument 2 = 1
argument 3 = {2}
argument 4 = 2
argument 5 = {3}
argument 6 = 3
Test mit $re{3}
argument 1 = {3}
argument 2 = 3
Test mit (?:$re){3}
argument 1 = {3}
argument 2 = 3
1
2
3
4
5
6
7
$s="\\Befehl{1}{2}{3}";
print "Test mit ((\$re)+)\n";
if ($s =~ /\\Befehl(($re)+)/g){
foreach $expr (1..$#-) {
print "\targument $expr = $$expr\n";
};
};
QuoteTest mit (($re)+)
argument 1 = {1}{2}{3}
argument 2 = {3}
argument 3 = {3}
argument 4 = 3
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
#! /usr/bin/perl # vim:ts=4 sw=4 sts=4 et nu fdc=3: use strict; use warnings; my @str = ( "bla bla \\mycommand{arg1}{arg2}{arg3} foo foo", "Will I work? \\whatever", "this will be wrong: \\cmd{arg1} {bad_style_arg}", ); my $re_command = qr/\\(\w+)/; my $re_argument = qr/{([^}]+)}/; for my $str (@str) { # prerequisite: only one command sequenz in $str if ( ( my ($cmd) = $str =~ $re_command ) && ( my @args = $str =~ m/$re_argument/g ) >= 0 ) { print "$cmd with ", scalar(@args), " arguments: @args\n"; } } __END__