由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个perl的替换问题。
相关主题
请教一个变态的regular expression 替换Python, Java, Perl, PHP,Ruby
perl能不能一次把一个str中的a替换成x,b替换成y? (转载)python能检查出space是一个还是两个吗?
any perl html parser to recommend?写Python 的苦恼之一:有人当c用,有人当bash用,有人当FP用。当然也有人当python用
现在哪些script最流行?perl 匹配问题
perl 问题求教问个关于正则表达式的超弱智问题...
Re: 问个google面试题 (转载)Perl 6 改动很大很恶心
python的问题,大拿帮忙看看请问macbook自带的perl怎样可以替换掉? (转载)
perl的文本处理大部分容易在python里实现吗?求教一个perl问题
相关话题的讨论汇总
话题: units话题: perl话题: 替换话题: sunits话题: hash
进入Programming版参与讨论
1 (共1页)
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我真得不懂。
1 (共1页)
进入Programming版参与讨论
相关主题
求教一个perl问题perl 问题求教
这个PERL表达式干啥的?Re: 问个google面试题 (转载)
why int** cannot convert to const int** ?python的问题,大拿帮忙看看
C的问题,高手请指点perl的文本处理大部分容易在python里实现吗?
请教一个变态的regular expression 替换Python, Java, Perl, PHP,Ruby
perl能不能一次把一个str中的a替换成x,b替换成y? (转载)python能检查出space是一个还是两个吗?
any perl html parser to recommend?写Python 的苦恼之一:有人当c用,有人当bash用,有人当FP用。当然也有人当python用
现在哪些script最流行?perl 匹配问题
相关话题的讨论汇总
话题: units话题: perl话题: 替换话题: sunits话题: hash