分页: 1 / 1
[问题]谁能解释一下这段话?
发表于 : 2007-01-14 23:38
由 5451vs5451
As with regular pattern matching, any capturing parentheses that are not matched
in a "split()" will be set to "undef" when returned:
@fields = split /(A)│B/, "1A2B3";
# @fields is (1, ’A’, 2, undef, 3)
发表于 : 2007-01-15 14:07
由 xki
如果是 ……
代码: 全选
@list = split/A/,"1A2A3A4A";
foreach (@list){
print "[",$_,"].";
}
输出结果就是:[1].[2].[3].[4].
如果是 ……
代码: 全选
@list = split/(A)/,"1A2A3A4A";
foreach (@list){
print "[",$_,"].";
}
输出结果就是:[1].[A].[2].[A].[3].[A].[4].[A].
可以理解为:括号对字符起到了“保留而不替换”的作用
发表于 : 2007-01-15 19:41
由 5451vs5451
假如有
代码: 全选
$string="f1 (abc), f2, f3 (def); f4";
怎样才能把它拆成
代码: 全选
@field=qw{f1 f2 f3 f4};
呢?
发表于 : 2007-01-16 22:39
由 xki
代码: 全选
#!/usr/bin/perl
my($str,@array);
$str="f1 (abc), f2, f3 (def); f4";
while ($str=~/(f\d)/g){
push @array,$1;
}
print "@array";