我需要用 Perl 中的一些其他内容(文本或二进制)替换二进制内容。
如果内容是文本,我可以使用 s/// 来替换内容。
my $txt = "tomturbo";
$txt =~ s/t/T/g; # TomTurbo
但如果内容是二进制的,那就不起作用了。
有没有简单的方法来处理?
Thanks!
Perl 的正则表达式引擎可以处理任意字符串。
s/t/T/g
与
s/\x{74}/\x{54}/g
meaning it will change the value of characters from 7416 to 5416. The regex engine doesn't care whether value 7416 means t
or something else.
In binary data, character A16 doesn't indicate the end of a line, so you'll want to use the s
flag, avoid the m
flag, and avoid using $
.
此外,您还会发现i
标志和内置字符类(例如\d
, \p{Letter}
等)除非您与解码文本字符串(Unicode 代码点字符串)进行匹配,否则无用,但您可以使用正则表达式引擎的任何功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)