免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5304 | 回复: 5
打印 上一主题 下一主题

Perl正则表达式处理指定汉字的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-03-20 10:37 |只看该作者 |倒序浏览
有段文字:

  例如:  外层短边拉伸:3MIL,长边收缩:5MIL。

现在我想处理的结果是:如果自动根据文字描述提取相应的参数:

如果出现:“短边” 关键字就输出为 X,“长边”关键字就输出为  Y.  "拉伸“ 为: +  ,”收缩“为:-  

那么上面的文字,处理结果应为:   X:3MIL,Y:-5MIL

但是在程序做正则表达式处理时,直接在表达式里面过滤指定汉字,程序过滤不了。请问高手,这个正则表达式应该怎么书写。谢谢!

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
2 [报告]
发表于 2015-03-20 12:17 |只看该作者
正常匹配if处理不行吗?

论坛徽章:
3
CU十二周年纪念徽章
日期:2013-10-24 15:41:34子鼠
日期:2013-12-14 14:57:19射手座
日期:2014-04-25 21:23:23
3 [报告]
发表于 2015-03-20 15:21 |只看该作者
本帖最后由 mcshell 于 2015-03-20 15:22 编辑
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. binmode( STDIN,  ':encoding(utf8)' );
  6. binmode( STDOUT, ':encoding(utf8)' );   
  7. binmode( STDERR, ':encoding(utf8)' );   
  8.   my $h = {
  9. '短边' => 'X',
  10. '长边' => 'Y',
  11. '拉伸' => '+',
  12. '收缩' => '-',
  13. ':' =>':'
  14. };
  15. my $str = '外层短边拉伸:3MIL,长边收缩:5MIL';
  16. for (keys %$h){
  17.         $str =~ s/$_/$h->{$_}/eg;
  18. }
  19.         $str=~ s/(.):/:$1/g;
  20.         $str=~ s/[\x{4e00}-\x{9fa5}]+//g;
  21.         print $str;
复制代码

论坛徽章:
0
4 [报告]
发表于 2015-03-22 22:41 |只看该作者
回复 3# mcshell


    你好,您的程序,我运行了。提示有错误信息。不知道是什么情况,请帮忙看下,谢谢!

C:\Perl\bin>perl aa.pl
Malformed UTF-8 character (unexpected non-continuation byte 0xec, immediately af
ter start byte 0xc9) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 3, after start byte 0xec) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xec, immediately af
ter start byte 0xc9) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 3, after start byte 0xec) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xd5, immediately af
ter start byte 0xca) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xcb, immediately af
ter start byte 0xd5) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xf5, immediately af
ter start byte 0xcb) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 4, after start byte 0xf5) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xd5, immediately af
ter start byte 0xca) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xcb, immediately af
ter start byte 0xd5) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (unexpected non-continuation byte 0xf5, immediately af
ter start byte 0xcb) in regexp compilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 4, after start byte 0xf5) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 2, after start byte 0xdf) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 2, after start byte 0xdf) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 2, after start byte 0xdf) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (1 byte, need 2, after start byte 0xdf) in regexp comp
ilation at aa.pl line 17.
Malformed UTF-8 character (fatal) at aa.pl line 20.

论坛徽章:
0
5 [报告]
发表于 2015-03-22 22:42 |只看该作者
回复 4# h97252


    Linux平台和WINDOWS平台我都测试了,都是提示这样的错误。

论坛徽章:
0
6 [报告]
发表于 2015-03-25 11:30 |只看该作者
回复 3# mcshell


        程序没问题,是我没处理好。现在已经可以运行OK。非常感谢MCSHELL的无私帮助!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP