- 论坛徽章:
- 0
|
本帖最后由 david_95 于 2016-06-02 17:00 编辑
我有一批文本,我想用一个哈希来动态匹配,找到匹配的行,为了方便大家调试,我写成以下
my @lines;
push @lines,"{table_name:cntech_cx_brands_table,brand_locale_name:\"CN\",brand_gl_id:\"194\",brand_gl_description:\"gl_beauty\",brand_standard_name:\"Hazeline 夏士莲\",brand_scope:\"DEFAULT\",brand_is_valid:true,brand_image_name:\"img16/template/logo/480240/logo_xiashilian_480240\",brand_store_url:\"\",brand_from_country:\"\",brand_rich_content:\"\",brand_priority:0,brand_extra_info:\"\",brand_comments:\"\",brand_is_new:false,brand_sm:\"\",brand_is_ags:0,audit_info:{created_by:anhongli,creation_date:2016-06-01T03:24:39.009-00:00},stage_version:\"cntech_cx_brandsProd1-10\"}";
push @lines,"{table_name:cntech_cx_brands_table,brand_locale_name:\"CN\",brand_gl_id:\"201\",brand_gl_description:\"gl_beauty\",brand_standard_name:\"Hazeline 夏士莲\",brand_scope:\"DEFAULT\",brand_is_valid:true,brand_image_name:\"img16/template/logo/480240/logo_xiashilian_480240\",brand_store_url:\"\",brand_from_country:\"\",brand_rich_content:\"\",brand_priority:0,brand_extra_info:\"\",brand_comments:\"\",brand_is_new:false,brand_sm:\"\",brand_is_ags:0,audit_info:{created_by:anhongli,creation_date:2016-06-01T03:24:39.009-00:00},stage_version:\"cntech_cx_brandsProd1-10\"}";
push @lines,"{table_name:cntech_cx_brands_table,brand_locale_name:\"CN\",brand_gl_id:\"194\",brand_gl_description:\"gl_beauty\",brand_standard_name:\"Hazeline 夏士莲\",brand_scope:\"DEFAULT\",brand_is_valid:true,brand_image_name:\"img16/template/logo/480240/logo_xiashilian_480240\",brand_store_url:\"\",brand_from_country:\"\",brand_rich_content:\"\",brand_priority:0,brand_extra_info:\"\",brand_comments:\"\",brand_is_new:false,brand_sm:\"\",brand_is_ags:0,audit_info:{created_by:anhongli,creation_date:2016-06-01T03:24:39.009-00:00},stage_version:\"cntech_cx_brandsProd1-10\"}";
my $hash_filter={brand_gl_id=>"201",brand_standard_name =>"夏士莲"};
我要匹配行为包含 brand_gl_id:“201” 并且 brand_standard_name:".*?夏士莲.*?” 的行,也就是第二行,数组@lines中的第二个
我的代码为以下, 无法匹配到:
while(my ($key,$value)=each %$hash_filter){
say $key.":".$value;
my $cond=quotemeta($key).":\".*?".quotemeta($value).".*?\"";
say $cond;
@lines=grep {$_=~/quotemeta($cond)/g} @lines;
}
于是我直接实验,把字符串填进去,就可以匹配到
@lines=grep {$_=~/brand_gl_id:".*?201.*?"/g}@lines;
@lines=grep {$_=~/brand_standard_name:".*?夏士莲.*?"/g}@lines;
弄不懂为什么, 打印输出 $cond ,就是我直接填入的字符串啊, 为什么匹配不到呢? 求大神指点
|
|