我有以下代码:
#!/usr/bin/perl
use warnings;
use strict;
my $SourceStr='Foo - Name: Rob Time: 11/2/2011 13:47:30 State: Prelim 3 Optional: Some stuff here';
#my $SourceStr='Foo - Name: Rob Time: 11/2/2011 13:47:30 State: Prelim 3';
my $RegEx = qr/Name: (.+) Time: (.+) State: (.+) Optional: (.+?)( |$)/;
if ($SourceStr =~ m/$RegEx/) {
print "1=[$1]\n";
print "2=[$2]\n";
print "3=[$3]\n";
print "4=[$4]\n";
}
当使用第一个 $SourceStr 运行时,它按预期工作。但是,对于被注释掉的第二个,有没有办法用空字符串填充 $4 ?
第一个字符串结果:
1=[Rob]
2=[11/2/2011 1:47:30 PM]
3=[3]
4=[Some stuff here]
第二个字符串结果:不匹配
想:
1=[Rob]
2=[11/2/2011 1:47:30 PM]
3=[3]
4=[]