gpt4 book ai didi

perl - 检测 Perl 字符串是否可打印

转载 作者:行者123 更新时间:2023-12-05 00:50:53 24 4
gpt4 key购买 nike

我正在尝试在 Perl 中打印递归数据结构以进行调试。散列数组的散列,诸如此类的东西......

它的一些基本数据元素是可打印的字符串,所以我正在打印它们。不幸的是,一些基本数据元素是二进制的(想想图像文件中的内容)。他们用乱码搞砸了我的调试输出。

我如何检测哪个是哪个,这样我就可以避免像字符串一样打印二进制文件?

(我知道 Data::Dumper。我的问题不是我是否应该复制该功能,而是关于如何区分文本和二进制字符串。)

最佳答案

perlrecharclass定义了这些字符类:

Any printable character, excluding a space. Any character that is graphical, that is, visible. This class consists of all alphanumeric characters and all punctuation characters.

Any printable character, including a space. All printable characters, which is the set of all graphical characters plus those whitespace characters which are not also controls.

因此您可以匹配不具有 Unicode 属性的字符(注意大写 P),例如:

/\P{XPosixPrint}/

我怀疑你真正想要的是检测控制字符,这会破坏终端(注意小写 p):

/\p{XPosixCntrl}/

关于perl - 检测 Perl 字符串是否可打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com