perl正则表达式匹配问题

软件和网站开发以及相关技术探讨
回复
wizardyhnr
帖子: 28
注册时间: 2007-05-06 10:56

perl正则表达式匹配问题

#1

帖子 wizardyhnr » 2007-08-30 19:40

写了下面的脚本
#!/usr/bin/perl
#
# Program to do the obvious
#
local $/ = undef;
$lines = <FILE>;
$file = "mylog";
open(INFO,$file);# Open the file
$lines = <INFO>;# Read it into an array
close(INFO);# Close the file
print $lines;# Print the array

print "It matches 2\n" if $lines=~ m/\x{e5b7b2}/s;

创建了文件mylog,内容如下:
------------------分割线,非内容
记录文件已经开始工作
weiyuan carried them out at 2007年 08月 23日 星期四 21:53:31 CST
这一行是用cat命令添加的
------------------分割线,非内容
文件采用utf8编码。

查到“已”字的utf8编码是0xe5b7b2,想要用上面的脚本测试一下perl匹配正则表达式,但是输出是不匹配,请问为什么呢?
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#2

帖子 BigSnake.NET » 2007-08-30 20:21

代码: 全选

#!/usr/bin/perl
#
# Program to do the obvious
#
local $/ = undef;
$lines = <FILE>;
$file = "mylog";
open(INFO,$file);# Open the file
$lines = <INFO>;# Read it into an array
close(INFO);# Close the file
print $lines;# Print the array

print "It matches 2\n" if $lines=~ m/已/s;
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
wizardyhnr
帖子: 28
注册时间: 2007-05-06 10:56

#3

帖子 wizardyhnr » 2007-08-30 20:33

直接就行?亏我还看了一下午的perl文档,那为什么/\x{...}/的形式不行呢?
头像
nihui
帖子: 1746
注册时间: 2006-10-21 12:03
来自: Shanghai
联系:

#4

帖子 nihui » 2007-08-30 21:29

[UTF8 "\x{.....}"]

貌似我还没看过文档....... :shock: :shock:

代码: 全选

#!/usr/bin/perl 
# 
# Program to do the obvious 
# 
local $/ = undef; 
$lines = <FILE>; 
$file = "mylog"; 
open(INFO,$file);# Open the file 
$lines = <INFO>;# Read it into an array 
close(INFO);# Close the file 
print $lines;# Print the array 

print "It matches 2\n" if $lines=~ m/[UTF8 "\x{e5b7b2}"]/s;
.

私人空间
附有 Magic Linux 2.1 最新开发日志~


미안하다, 사랑한다


欢迎来 #xfce-cn 中文频道~~
wizardyhnr
帖子: 28
注册时间: 2007-05-06 10:56

#5

帖子 wizardyhnr » 2007-08-31 11:47

嘎嘎,果然有效,谢谢,不过老外的文档每次涉及到处理非ascii字符的时候总有点不清不楚,不看也罢,浪费时间,原来的文档里面没说要加utf8前缀的。
回复