d**d 发帖数: 389 | 1 $item =~ s/<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>/my $units; if
($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$units";/gsie;
请问这个语句是在用什么东西替换前面的那个字符串?
Perl还可以在替换的当中定义变量和语句?
非常感谢。 |
N***m 发帖数: 4460 | 2 终于见到传说中的perl了。
【在 d**d 的大作中提到】 : $item =~ s/<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>/my $units; if : ($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$units";/gsie; : 请问这个语句是在用什么东西替换前面的那个字符串? : Perl还可以在替换的当中定义变量和语句? : 非常感谢。
|
d****p 发帖数: 685 | 3 This is a complex regrex - involving non greedy search and back reference. I
guess you missed a double quote.
You can expand variable in search/replace components but you cannot define
new variables/statements. This example merely translates a xml element into
a perl statement for evaluation.
【在 d**d 的大作中提到】 : $item =~ s/<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>/my $units; if : ($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$units";/gsie; : 请问这个语句是在用什么东西替换前面的那个字符串? : Perl还可以在替换的当中定义变量和语句? : 非常感谢。
|
I*****y 发帖数: 602 | 4 don't think he missed a double quote.
I
into
【在 d****p 的大作中提到】 : This is a complex regrex - involving non greedy search and back reference. I : guess you missed a double quote. : You can expand variable in search/replace components but you cannot define : new variables/statements. This example merely translates a xml element into : a perl statement for evaluation.
|
d****p 发帖数: 685 | 5 Yep you are right - I read it wrong.
In addition, the above regex has an "e" modifier which enables evaluating
the replacement component. So the variables (like $units and the hash)
therein should be already declared in prior.
【在 I*****y 的大作中提到】 : don't think he missed a double quote. : : I : into
|
g***l 发帖数: 2753 | 6 是不是用“my $units; if($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$
units";”的返回值来替换前面的字符串? 是不是就是用 1 (Number one)来替换?
【在 d**d 的大作中提到】 : $item =~ s/<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>/my $units; if : ($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$units";/gsie; : 请问这个语句是在用什么东西替换前面的那个字符串? : Perl还可以在替换的当中定义变量和语句? : 非常感谢。
|
d****p 发帖数: 685 | 7 Yep - if successfully evaluated.
3$
【在 g***l 的大作中提到】 : 是不是用“my $units; if($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$ : units";”的返回值来替换前面的字符串? 是不是就是用 1 (Number one)来替换?
|
g***l 发帖数: 2753 | 8 还会有失败的时间?我以为结果都是1呢。
什么情况下会出现不是1的情形? if($2)是FALSE的情况?呵呵
【在 d****p 的大作中提到】 : Yep - if successfully evaluated. : : 3$
|
d****p 发帖数: 685 | 9 What if there is a compile error.
And it is now always 1 - it is the return value of the last statement of the
list.
【在 g***l 的大作中提到】 : 还会有失败的时间?我以为结果都是1呢。 : 什么情况下会出现不是1的情形? if($2)是FALSE的情况?呵呵
|
p****o 发帖数: 1340 | 10 怯生生的说一句,我以前真的学过perl的。。。不过这个perl我真得不懂。
【在 N***m 的大作中提到】 : 终于见到传说中的perl了。
|
d****p 发帖数: 685 | 11 $item =~ s/<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>/my $units; if
($2) { $units = " $2"; } push @{$exit_hash{$1}}, "$3$units";/gsie;
The pattern is
<([^\s>]+)(?:\sUnits="([^"]+)"+)?>(.*?)<\/\1>
c
So ([^\s>])+ -> a -> \1 and ensures pair
(.*?) -> c
([^"]+ -> b
?: tells engine don't capture the whole (Units="b") block so it cannot be
referenced outside of this regex via $
The (Units=b) occurs at most once. The closing double quote for "b could
occur multiple times.
【在 p****o 的大作中提到】 : 怯生生的说一句,我以前真的学过perl的。。。不过这个perl我真得不懂。
|