- 论坛徽章:
- 0
|
我要从excel中取数据,用正则寻找特定行,方法为$temp=~/Li含量/,匹配/Li/则没问题,说明是中文引起的,因此用以下代码调查原因。- if(defined($_[0]->Cells($x,'B')->{Value})){
- $temp = $_[0]->Cells($x,'B')->{Value};
- print $temp," length is:",length($temp),"\n";
- $temp = decode("gb2312",$temp);
- print $temp,"length is:",length $temp,"\n";
- my @tempa = split //,$temp;
- print join("\t",@tempa),"\n";
- foreach (@tempa){$_=encode("cp936",$_);}
- $temp = join ("",@tempa);
- print @tempa,"scalar is:",scalar(@tempa),"\n";
- print $temp," length is:", length($temp),"\n";
- }
复制代码 结果如下:
---
锂盐 length is:4
Wide character in print at test.pl line 36.
閿傜洂length is:2
Wide character in print at test.pl line 38.
閿?鐩?
锂盐scalar is:2
锂盐 length is:4
----
不能理解的是,为何split后为两个元素,为何join后长度又变成了4,最终仍然无法正确匹配中文。excel到底是什么编码,如何才能正确进行中文匹配呢? |
|