用perl给汉字加空格,失败,求助

xudekuan

Moderator
1、用editplus和emeditor,通过正则表达式,将
([^\x80-\xff])
替换成
\1 (1后面有个空格)


能够将给汉字后面加上一个空格。


2、反向操作,亦即去除空格,也成功
([^\x80-\xff]) (右括号“)”后有一个空格)
替换为
\1(1后没有空格)

3、用perl,可以去除空格,不过和editplus有点小小区别,代码如下:

open output, ">ttt.txt";
open input, "in.txt";
while (<input>)
{
s/([\x80-\xff]) +/$1/g;#右括号“)”后有一个空格,而且反斜线“\”前面没有^,这是不同
print output $_;
}




4、但是用perl,却不能加空格,代码如下:

open output, ">ttt.txt";
open input, "in.txt";
while (<input>)
{
s/([\x80-\xff])/$1 /g; #1后有一个空格,
print output $_;
}

不知这是为啥?请高手指点。
输入文本:
天天向上 。来了。This is an example. That is another example.
好好学习。


输出文本:
?豢澶╁ぉ鍚戜笂 銆傛潵 浜嗐?俆his is an example. That is another example.
?ソ濂藉?涔犮?
 
回复: 用perl给汉字加空格,失败,求助

是不是编码的问题?使用 utf-8 的编码试试?
 
Back
顶部