我想在以下条件下在CSV文件中查找和替换日期:
1)第一列为空白,如“”,“”,
1a)由于前两列中的文字
,$ case [1]不匹配2)接下来的6列中的每一列都可能包含日期,如下面的$ case [0]
2a)$ case [2]不匹配,因为所有6列都是空白
my @case = (
'"","","","1/2/2012","","","","",="12345678"',
'"Add","New","1/1/2012","1/2/2012","","","",""="0987654"',
'"","","","","","","","",="91234567"'
);
我使用了以下代码,但它错误地匹配$ case [2]并影响脚本的效率:
my $argFind = (qr/^"","",("[\d\/]*",){6}(.*)/);
$replace = '"","","","","","","","",';
if (grep(/$argFind/,@case))
{
s/$argFind/$replace$2/ for @case;
#write file
}
最终结果应该是:
$case = [
'"","","","","","","","",="12345678"',
'"Add","New","1/1/2012","1/2/2012","","","",""="0987654"',
'"","","","","","","","",="91234567"'
];
答案 0 :(得分:2)
我相信您应该真正使用Text::CSV
从CSV记录中检索数据值列表。然后,您可以单独检查字段以检查它们是否符合您的要求。
但只要数据自动生成并保持良好状态,您就可以尝试
qr[ ^ (?: "", ){2} (?: " (?: \d\d?/\d\d?/\d\d\d\d )? ", ){6} ]x;
whcih找到两个空字段,后跟六个字段,这些字段为空或包含看起来像日期的内容。该计划演示
use strict;
use warnings;
my @case = (
'"","","","1/2/2012","","","","",="12345678"',
'"Add","New","1/1/2012","1/2/2012","","","",""="0987654"',
'"","","","","","","","",="91234567"'
);
my $argFind = qr[ ^ (?: "", ){2} (?: " (?: \d\d?/\d\d?/\d\d\d\d )? ", ){6} ]x;
my $replace = '"",' x 8;
for (@case) {
print "$_\n" if s/$argFind/$replace/;
}
<强>输出强>
"","","","","","","","",="12345678"
"","","","","","","","",="91234567"
答案 1 :(得分:0)
嗯,我得到了你的最终结果:
qr/^(?:"",){3}"\d\d?\/\d\d?\/\d{4}",(?:"",){4}/;
答案 2 :(得分:0)
感谢您的所有投入。我打算用这样的东西作为解决方案:
use warnings;
use Data::Dumper;
my @case = (
'"","","","1/2/2012","","","","",="12345678"',
'"","","1/1/11","","","","","",="12345678"',
'"","","","1/2/13","","4/3/2010","","",="987654"',
'"","","","","","","","1/3/2012",="567890"',
'"Add","New","1/1/2012","1/2/2012","","","","",="0987654"',
'"","","","","","","","",="91234567"'
);
my $argFind = (qr/^"","",("[\d\/]*",){6}/);
my $replace = '"",' x 8;
for (@case) {
unless (m/$replace/) {
s/$argFind/$replace/;
# Set flag to write file after loop
}
}
warn Dumper \@case;