perl 中对于GBK编码的字符的处理方法

perl unload gbk oracle 数据库 perl unload gbk Oracle 数据库 use Encode; if ( $#ARGV < 0 ){ print "请输入一个文件\n"; exit(-1); } $var = $ARGV[0]; open (FILE ,"<","$var") || die "can open file$!\n"; while () { $str = e 阅读详情
perl 中对于GBK编码的字符的处理方法

#返回字符串长度

use String::Multibyte;
$gbk_str="上大";
$gbk= String::Multibyte->new('GBK');
$gbk_len = $gbk->length($gbk_str);

Constructor

new(CHARSET)"> $mbcs = String::Multibyte->new(CHARSET)

new(CHARSET,_VERBOSE)"> $mbcs = String::Multibyte->new(CHARSET, VERBOSE)

CHARSET is the charset name; exactly speaking, the file name of the definition file (without the suffix .pm). It returns the instance to tell methods in which charset the specified strings should be handled.

CHARSET may be a hashref; this is how to define a charset without .pm file.

    # see perlfaq6  :-)
    my $martian  = String::Multibyte->new({
        charset => "martian",
        regexp => '[A-Z][A-Z]|[^A-Z]',
    });

If true value is specified as VERBOSE, the called method (excepting islegal) will check its arguments and carps if any of them is not legally encoded.

Otherwise such a check won't be carried out (saves a bit of time, but unsafe, though you can use the islegal method if necessary).

Check Whether the String is Legal

检测字符串是否是合法的GBK字符

islegal(LIST)"> $mbcs->islegal(LIST)

Returns a boolean indicating whether all the strings in arguments are legally encoded in the concerned charset. Returns false even if one element is illegal in LIST.

Length

length(STRING)"> $mbcs->length(STRING)

Returns the length in characters of the specified string.

Reverse

字符串倒置

strrev(STRING)"> $mbcs->strrev(STRING)

Returns a reversed string in characters.

Search

搜索

index(STRING,_SUBSTR)"> $mbcs->index(STRING, SUBSTR)

index(STRING,_SUBSTR,_POSITION)"> $mbcs->index(STRING, SUBSTR, POSITION)

Returns the position of the first occurrence of SUBSTR in STRING at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string.

If the substring is not found, returns -1.

反向搜索

rindex(STRING,_SUBSTR)"> $mbcs->rindex(STRING, SUBSTR)

rindex(STRING,_SUBSTR,_POSITION)"> $mbcs->rindex(STRING, SUBSTR, POSITION)

Returns the position of the last occurrence of SUBSTR in STRING at or after POSITION. If POSITION is specified, returns the last occurrence at or before that position.

If the substring is not found, returns -1.

strspn(STRING,_SEARCHLIST)"> $mbcs->strspn(STRING, SEARCHLIST)

搜索第一个串中不包含在第二个串的字符集合中的字符的位置

Returns returns the position of the first occurrence of any character not contained in the search list.

  $mbcs->strspn("+0.12345*12", "+-.0123456789");
  # returns 8.

If the specified string does not contain any character in the search list, returns 0.

The string consists of characters in the search list, the returned value equals the length of the string.

SEARCHLIST can be an ARRAYREF. e.g. if a charset treats CRLF as a single character, "/r/n" is a one-element list of only "/r/n". A two-element list of "/r" and "/n" can be given as ["/r", "/n"] (of course "/n/r" is also ok since the character order of SEARCHLIST doesn't matter in strspn).

strcspn(STRING,_SEARCHLIST)"> $mbcs->strcspn(STRING, SEARCHLIST)

Returns returns the position of the first occurrence of any character contained in the search list.

If the specified string does not contain any character in the search list, the returned value equals the length of the string.

SEARCHLIST can be an ARRAYREF. e.g. if a charset treats CRLF as a single character, "/r/n" is a one-element list of only "/r/n". A two-element list of "/r" and "/n" can be given as ["/r", "/n"] (of course "/n/r" is also ok since the character order of SEARCHLIST doesn't matter in strcspn).

Substring

子串

substr(STRING_or_SCALAR_REF,_OFFSET)"> $mbcs->substr(STRING or SCALAR REF, OFFSET)

substr(STRING_or_SCALAR_REF,_OFFSET,_LENGTH)"> $mbcs->substr(STRING or SCALAR REF, OFFSET, LENGTH)

substr(SCALAR,_OFFSET,_LENGTH,_REPLACEMENT)"> $mbcs->substr(SCALAR, OFFSET, LENGTH, REPLACEMENT)

It works like CORE::substr, but using character semantics of multibyte charset encoding.

If the REPLACEMENT as the fourth argument is specified, replaces parts of the SCALAR and returns what was there before.

You can utilize the lvalue reference, returned if a reference of scalar variable is used as the first argument.

    ${ $mbcs->substr(/$str,$off,$len) } = $replace;

        works like

    CORE::substr($str,$off,$len) = $replace;

The returned lvalue is not multibyte character-oriented but byte-oriented, then successive assignment may lead to odd results.

Split

分割

strsplit(SEPARATOR,_STRING)"> $mbcs->strsplit(SEPARATOR, STRING)

strsplit(SEPARATOR,_STRING,_LIMIT)"> $mbcs->strsplit(SEPARATOR, STRING, LIMIT)

This function emulates CORE::split, but splits on the SEPARATOR string, not by a pattern.

If not in list context, only return the number of fields found, but does not split into the @_ array.

If empty string is specified as SEPARATOR, splits the specified string into characters.

  $bytes->strsplit('', 'This is perl.', 7);
  # ('T', 'h', 'i', 's', ' ', 'i',  's perl.')

Character Range

返回一定内码值区域内的所有字符的列表

mkrange(CHARLIST,_ALLOW_REVERSE)"> $mbcs->mkrange(CHARLIST, ALLOW_REVERSE)

Returns the character list (not in list context, as a concatenated string) gained by parsing the specified character range.

The result depends on the the character order for the concerned charset. About the character order for each charset, see its definition file.

If the character order is undefined in the definition file, returns an identical string with the specified string.

A character range is specified with a hyphen ('-', but exactly speaking, $obj->{hyphen}).

The backslashed combinations '/-' and '//' (exactly speaking, "$obj->{escape}$obj->{hyphen}" and "$obj->{escape}$obj->{escape}") are used instead of the characters '-' and '/', respectively. The hyphen at the beginning or the end of the range is also evaluated as the hyphen itself.

For example, $mbcs->mkrange('+/-0-9A-F') returns ('+', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F') and scalar $mbcs->mkrange('A-P') returns 'ABCDEFGHIJKLMNOP'.

If true value is specified as the second argument, reverse character ranges such as '9-0', 'Z-A' are allowed.

  $bytes = String::Multibyte->new('Bytes');
  $bytes->mkrange('p-e-r-l', 1); # ponmlkjihgfefghijklmnopqrqponml

Transliteration

搜索并且替换

strtr(STRING_or_SCALAR_REF,_SEARCHLIST,_REPLACEMENTLIST)"> $mbcs->strtr(STRING or SCALAR REF, SEARCHLIST, REPLACEMENTLIST)

strtr(STRING_or_SCALAR_REF,_SEARCHLIST,_REPLACEMENTLIST,_MODIFIER)"> $mbcs->strtr(STRING or SCALAR REF, SEARCHLIST, REPLACEMENTLIST, MODIFIER)

Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list.

If a reference of scalar variable is specified as the first argument, returns the number of characters replaced or deleted; otherwise, returns the transliterated string and the specified string is unaffected.

If 'h' modifier is specified, returns a hash of histogram in list context; a reference to hash of histogram in scalar context;

SEARCHLIST and REPLACEMENTLIST

Character ranges (internally utilizing mkrange()) are supported.

If the REPLACEMENTLIST is empty (specified as '', not undef, because the use of uninitialized value causes warning under -w option), the SEARCHLIST is replicated.

If the replacement list is shorter than the search list, the final character in the replacement list is replicated till it is long enough (but differently works when the 'd' modifier is used).

SEARCHLIST and REPLACEMENTLIST can be an ARRAYREF. e.g. if a charset treats "/r/n" (CRLF) as a single character, "/r/n" is a one-element list of only "/r/n". A two-element list of "/r" and "/n" should be given as ["/r", "/n"]. Of course "/n/r" is also ok but the character order is different; cf. strtr($str, ["/r", "/n"], ["/n", "/r"]) that swaps "/n" and "/r".

Each elements of ARRAYREF can include character ranges (the modifiers R and r affect their evaluation as usual).

["A-C", "h-z"] is evaluated like "A-Ch-z" if charset does not include grapheme "Ch". The former prevents "C" and "h" from evaluation as "Ch" even if the charset included grapheme "Ch".

MODIFIER

    c   Complement the SEARCHLIST.
    d   Delete found but unreplaced characters.
    s   Squash duplicate replaced characters.
    h   Return a hash (or a hashref) of histogram.
    R   No use of character ranges.
    r   Allows to use reverse character ranges.
    o   Caches the conversion table internally.

If 'R' modifier is specified, '-' is not evaluated as a meta character but hyphen itself like in tr'''. Compare:

  $mbcs->strtr("90 - 32 = 58", "0-9", "A-J");
    # output: "JA - DC = FI"

  $mbcs->strtr("90 - 32 = 58", "0-9", "A-J", "R");
    # output: "JA - 32 = 58"
    # cf. ($str = "90 - 32 = 58") =~ tr'0-9'A-J';
    # '0' to 'A', '-' to '-', and '9' to 'J'.

If 'r' modifier is specified, reverse character ranges are allowed. e.g.

   $mbcs->strtr($str, "0-9", "9-0", "r")

     is equivalent to

   $mbcs->strtr($str, "0123456789", "9876543210")

Caching the conversion table

If 'o' modifier is specified, the conversion table is cached internally. e.g.

  foreach (@source_strings) {
    print $mbcs->strtr($_, $from_list, $to_list, 'o');
  }

will be almost as efficient as this:

  $trans = $mbcs->trclosure($from_list, $to_list);

  foreach (@source_strings) {
    print &$trans($_);
  }

You can use whichever you like.

Without 'o',

  foreach (@source_strings) {
    print $mbcs->strtr($_, $from_list, $to_list);
  }

will be very slow since the conversion table is made whenever the function is called.

Generation of the Closure to Transliterate

返回一个指向一个搜索规则的函数的引用

trclosure(SEARCHLIST,_REPLACEMENTLIST)"> $mbcs->trclosure(SEARCHLIST, REPLACEMENTLIST)

trclosure(SEARCHLIST,_REPLACEMENTLIST,_MODIFIER)"> $mbcs->trclosure(SEARCHLIST, REPLACEMENTLIST, MODIFIER)

Returns a closure to transliterate the specified string. The return value is an only code reference, not blessed object. By use of this code ref, you can save yourself time as you need not specify arguments every time.

  my $trans = $mbcs->trclosure($from_list, $to_list);
  print &$trans ($string); # ok to perl 5.003
  print $trans->($string); # perl 5.004 or better

The functionality of the closure made by trclosure() is equivalent to that of strtr(). Frankly speaking, the strtr() calls trclosure() internally and uses the returned closure.

SEARCHLIST and REPLACEMENTLIST can be an ARRAYREF same as strtr().

CAVEAT

$[

This modules supposes $[ is always equal to 0, never 1.

Grapheme manipulation

Since v. 1.01, manipulation of sequence of graphemes is to be supported.

In a grapheme-oriented manipulation, notice that the beginning and the end of a string are always on a grapheme boundary.

E.g. imagine a grapheme set where a grapheme comprises either a leading latin capital letter followed by one or more latin small letters, or a single byte. Such a set can be define as below.

   $gra = String::Multibyte->new({
         regexp => '[A-Z][a-z]*|[/x00-/xFF]',
      });

Think about $gra->index("Perl", "Pe"). As both "Perl" and "Pe" are a single grapheme, they are not equal to each other. So the result of this must be -1 (meaning no match).

Perl 的gbk.enc文件 网上只有gb2312.enc,自己造了一个gbk.enc,传上来共享给大家。 立即下载

相关推荐

Perl GB2312.enc

Perl 的GB2312.enc 文件,网上很多链接都不能下载了。好不容易找到一个,给大家分享。

系统中乱码报错的解决办法

[root@localhost Python-3.7.1]# make &amp;&amp; make install 安装中可能遇到Fatal Python error: initfsencoding: Unable to get the locale encoding LookupError: unknown encoding: GBK这种错误,需要先把系统的编码修改成utf8,再进行安装 [...

wyqwilliam的博客 1189

谷歌Gemini模型免费API调用全攻略:从注册到实战(附代码示例)

本文详细介绍了如何免费调用谷歌Gemini模型的API,从注册获取API密钥到实战应用的全过程。内容涵盖环境准备、基础API调用、多模态处理、高级功能及生产环境实践,帮助开发者高效利用Gemini模型进行代码生成和复杂任务处理。附有代码示例,适合AI开发者和技术爱好者。

weixin_29212429的博客 1597

Perl中文乱码

乱码问题,总是个问题。 在上次为了用Perl读Excel文件,但是文件名是中文,而且内容也包括中文,结果读出来的是乱码,为了演示那个例子,当时创建了一个不包括中文的xls文件。但中文乱码问题一直没有去解决,虽然我知道这对于神奇的Perl来说,很容易。 下面就来初步的看看关于这个问题的解决方法。 1.全局统一编码 use utf8; use open FD,":en...

weixin_34320159的博客 411

ICESat-2数据下载及处理分析随笔

ICESat-2全称是Ice,Cloud and land Elevation Satellite -2。看名字我们能知道它的任务是测ice,cloud和land elevation,而且这是一个Satellite,是一颗卫星。那么这里我们就要分清ICESat-2和ATLAS的关系,ICESat-2是星载平台,是一个space observatory,ATLAS是这个星载平台上搭载的单光子激光雷达系统。

u011322358的博客 1万+

铭飞MCMS源码部署

文章目录前言一、下载铭飞源码二、部署源码1.使用IDEA打开源码2.配置tomcat环境2.1下载tomcat并测试是否可以成功启动(此处不赘述)2.2在IDEA中安装tomcat环境并配置3.添加数据库,并配置3.1新建数据库3.2 导入sql配置三、启动测试 前言 铭飞MCMS基于IDEA、tomcat和mysql的环境搭建。 提示:以下是本篇文章正文内容,下面案例可供参考 一、下载铭飞源码 gitee:https://gitee.com/mingSoft/MCMS/releases(各种版本按需下

MARS_ZORO的博客 4062

perl文件编码处理问题

在看perl时,发现文件编码处理问题不是很懂。BG了下,发现还是有点东东,so,收藏下: --------------------------1----------------------------------- 一个脚本用于分析应用程序的日志,但是在这些日志文件中,有的编码是utf-8的,有的编码是gbk的.对于utf-8的日志文件,需要将内容转换成gbk的编码,这样看到的中文才不至于乱码

4449

Geant4 学习笔记(一):实现multi-thread多线程

简单修改实现Geant4多线程

weixin_52417815的博客 6083

Perl对中文的处理问题

发信人: chaoslawful (Skeleton Warrior), 信区: Perl 标 题: 关于Perl对中文的处理问题 发信站: BBS 水木清华站 (Sat Oct 25 12:17:27 2003), 转信 大家经常使用正则表达式的字符集从字符串中抽取或排除中文字符,但是这样做很费事,效果也并不是很理想。实际上Perl从5.6开始已经开始在内部使用 utf8编码来表示字符...

weixin_30852367的博客 215

行人重识别(ReID)基础知识入门

行人重识别(Person re-identification)主要目的是针对出现在监控摄像头内的某个目标行人,准确快速地从监控网络其他摄像头内的大量行人中将这个目标行人标识出来。如图所示一个区域有多个摄像头拍摄视频序列,ReID的要求对一个摄像头下感兴趣的行人,检索到该行人在其他摄像头下出现的所有图片。

用于记录 1万+

从芯片原厂到BSP工程师:技术转型与核心能力解析

嵌入式系统开发中,BSP(板级支持包)作为连接硬件与操作系统的关键层,其开发能力直接影响设备稳定性与性能。理解Linux内核架构(如内存管理、进程调度)和基础接口协议(I2C/SPI/USB)是BSP工程师的核心技能。在芯片验证与驱动开发场景中,多核异构系统调试与稳定性问题排查(如死机分析)展现了工程师的实战能力。随着物联网与智能设备发展,掌握从硬件适配到系统优化的全链路技术栈,成为职业发展的关键突破点。

weixin_36012152的博客 599

utf8转gbk出现问号的分析,浅谈perl对二进制的处理

utf8转gbk出现问号的分析,浅谈perl对二进制的处理 2011-07-20 16:42 有时候我们需要将utf8转成gbk,UTF-8用1到6个字节编码UNICODE字符,收录了超过10万个字符,BMP部分也有六万多个字符 而在进行编码转换时,我们往往需要转换为GBK编码进行后续处理,很多网页在转换后,会发现出现大量连续的问号:???????? 这些恶心的

2666

C#实现每日定时执行制定程序(附完整源码)

C#实现每日定时执行制定程序(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 862

Ubuntu转换文件编码格式(gbk to utf-8 )

把 1.txt 从 gbk 转成 utf-8 并保存为 2.txt iconv -f gbk -t utf-8 1.txt > 2.txt 注意:报告错误(非法字符)的话,加 -c 参数忽略错误 或者 使用 perl 的 convmv convmv -f

西涛offbye-移动全栈技术博客 5098

RK3588芯片CAN总线调试秘籍:SocketCAN与错误帧攻略

RK3588芯片在嵌入式领域凭借8nm工艺和强大性能成为核心控制器。本文详细介绍了其CAN总线配置流程,包括硬件连接、内核驱动配置及SocketCAN命令操作,并深入解析了CAN通信中的错误帧机制及分析方法。通过智能工厂案例,展示了从问题定位到解决方案的全过程,强调了稳定通信对工业应用的重要性。文章为RK3588的CAN总线应用提供了实用技术参考,并对未来智能诊断技术在该领域的应用进行了展望。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 511

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

perl split 中文 乱码问题

用split分割句子中的中文字符,本地两台机器完全没有问题,服务器死活都是乱码(这台机器默认编码的确是gbk) 后来做个尝试,句子decode,如果需要分割符,分隔符也要decode,输出的时候要encode,就正常了。 中间状态时乱码,那是perl自己认识的字节流,始、终状态是字符串。 use strict; use Encode; #输入文件是GBK编码 open(In," wh

++ 2202

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

关于perl中中文乱码的解决办法

今天在测试lwp模块的时候,发现读取的网站内容含有中文的输出后都是乱码,于是学习了一下perl的Encode模块,写一下总结,做个备忘,也希望能对其他的朋友有帮助!     先来学习一下perl中编码的基础知识:在Perl看来, 字符串只有两种形式。一种是octets, 即8位序列, 也就是我们通常说的字节数组。另一种utf8编码的字符串, perl管它叫string。 也就是说: Perl只认

kongjunlongaa的博客 6186

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

关于如何解决Perl输出中文乱码问题

本篇文章转载自百度经验 原文:https://jingyan.baidu.com/article/4f7d5712fd36301a21192744.html 相信很多小伙伴在使用Perl语言的时候,经常会遇到这样一个问题,Perl不支持中文?perl输入或者输出中文会出现乱码。 这时怎么办呢?下面小编就教小伙伴们如何解决这个问题。 就以windows上的ActivePerl为例。 ...

z2bns的博客 3371

ubuntu下 gbk转utf-8

Ubuntu gbk > utf-8转换   把 1.txt 从 gbk 转成 utf-8 并保存为 2.txt   iconv -f gbk -t utf-8 1.txt > 2.txt   注意:报告错误(非法字符)的话,加 -c 参数忽略错误   或者   使用 perl 的 convmv   convmv -f gb2312 -t utf8 -r --notest

雪飞_海 2877

perl中utf-8编码的处理

为了比较方便,考虑这样一个应用:把html页面中的所有非汉字字符全部去掉。 这里顺便告诉大家一个秘诀,只要文本被perl 按正确编码解释后,利用/w就可以匹配一个字母、数字、_、汉字,这个特性是不是很方便,所以我们只要用如下两次正则表达式就可以去掉所有非汉字字符,包括全角的一些标点(@#$%<,())也能去的很干净:   $str =~ s/[^w]//g; $str =~

兴趣,激情 3256

讨论perl中的utf-8编码处理

 为了比较方便,考虑这样一个应用:把html页面中的所有非汉字字符全部去掉。这里顺便告诉大家一个秘诀,只要文本被perl 按正确编码解释后,利用/w就可以匹配一个字母、数字、_、汉字,这个特性是不是很方便,所以我们只要用如下两次正则表达式就可以去掉所有非汉字字符,包括全角的一些标点(@#$%<,())也能去的很干净: $str =~ s/[^w]//g;$str =~ s/[0

oyd的专栏 5731

Perl 的 utf8 与编码处理

<br />Perl 的 utf8 与编码处理<br /><br />  本文基于笔者查阅的 perldoc 和试验结果。鉴于经常有人被编码问题困扰,现笔者把所了解的关于 Perl utf8 与编码处理的资料总结一下。由于所知有限,可能会有讲错的地方,如有需要,可自行查阅:<br />perldoc Encode<br />perldoc PerlIO<br />perldoc utf8<br />perldoc encoding<br />下面用到这几个函数,能提供有用的帮助:<br />Encode::

Tron的专栏 3406

Perl 输出中文乱码问题解决方法

很多小伙伴在使用 Perl 语言的时候,经常会遇到这样一个问题,Perl 不支持中文?perl 输入或者输出中文会出现乱码。这时怎么办呢? 方法/步骤 就以 windows 上的 ActivePerl 为例。 代码特别简单,就是一句: print "你好"; 却出现了乱码,如图。 遇到这样的问题,我们要怎么办? 其实就是 perl 编码的问题,这其实和我们操作系统的编码有关。...

u014389734的博客 1121

perl对中文的处理

转载自:http://blog.youkuaiyun.com/jallin2001/article/details/4808618 大家经常使用正则表达式的字符集从字符串中抽取或排除中文字符,但是这样做很费事,效果也并不是很理想。实际上Perl从5.6开始已经开始在内部 使用 utf8编码来表示字符,也就是说对中文以及其他语言字符的处理应该是完全没有问题的。关键在于目前使用的编辑器以及文件格式并不都能很好地

HyEVERYONE的专栏 1332
上一篇: NTA终于发了第一个release
下一篇: 多个flex分析器的整合
RobinKin
博客等级 码龄22年 17粉丝 89原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值