我尝试使用perl解析字符串并将匹配项放入数组中。
实施例。 " FUNC1(VALUE1)VALUE1,VALUE2,FUNC2(FUNC1(VALUE3))VALUE3,VALUE4,FUNC3(VALUE5)VALUE5"
输出:
FUNC1(VALUE1) VALUE1
VALUE2
FUNC2(FUNC1(VALUE3)) VALUE3
VALUE4
FUNC3(VALUE5) VALUE5
我的代码:
my $in = "FUNC1(VALUE1) VALUE1, VALUE2, FUNC2(FUNC1(VALUE3)) VALUE3, VALUE4, FUNC3(VALUE5) VALUE5";
my @cols = ($in =~ /((?&full_m)),?
(?(DEFINE)
(?<full_m>(?&full_f)|(?&word))
(?<full_f>(?&func)\s(?&word))
(?<func>(?&word)\((?&worf)\))
(?<worf>(?&func)|(?&word))
(?<word>\s*\w+\s*)
)/gx);
print "$in\n";
my $count = 1;
foreach (@cols) {
print "$count: $_\n";
++$count;
}
问题是我得到了比赛,但之后还有5场空比赛。
1: FUNC1(VALUE1) VALUE1
2:
3:
4:
5:
6:
7: VALUE2
8:
9:
10:
11:
12:
13: FUNC2(FUNC1(VALUE3)) VALUE3
14:
15:
16:
17:
18:
19: VALUE4
20:
21:
22:
23:
24:
25: FUNC3(VALUE5) VALUE5
26:
27:
28:
29:
30:
答案 0 :(得分:1)
除了它只是将组1存储到col的数组中之外,这是相同的。
my $in = "FUNC1(VALUE1) VALUE1, VALUE2, FUNC2(FUNC1(VALUE3)) VALUE3, VALUE4, FUNC3(VALUE5) VALUE5";
my @cols;
while ($in =~ /((?&full_m)),?(?(DEFINE)(?<full_m>(?&full_f)|(?&word))(?<full_f>(?&func)\s(?&word))(?<func>(?&word)\((?&worf)\))(?<worf>(?&func)|(?&word))(?<word>\s*\w+\s*))/gx)
{
push @cols, $1;
}
print "$in\n";
my $count = 1;
foreach (@cols) {
print "$count: $_\n";
++$count;
}
输出
FUNC1(VALUE1) VALUE1, VALUE2, FUNC2(FUNC1(VALUE3)) VALUE3, VALUE4, FUNC3(VALUE5) VALUE5
1: FUNC1(VALUE1) VALUE1
2: VALUE2
3: FUNC2(FUNC1(VALUE3)) VALUE3
4: VALUE4
5: FUNC3(VALUE5) VALUE5
为了更好地查看正则表达式,需要格式化
( (?&full_m) ) # (1)
,?
(?(DEFINE)
(?<full_m> # (2 start)
(?&full_f)
| (?&word)
) # (2 end)
(?<full_f> # (3 start)
(?&func) \s (?&word)
) # (3 end)
(?<func> # (4 start)
(?&word) \( (?&worf) \)
) # (4 end)
(?<worf> # (5 start)
(?&func)
| (?&word)
) # (5 end)
(?<word> \s* \w+ \s* ) # (6)
)