正则表达式匹配特殊字符

regex to match special character

一个文本文件中有很多特殊字符(行终止符:LF;文件编码:utf-8)我正在处理,其中两个是和。它们对应的十六进制代码是 \xf4\x80\x91\x9a\xf4\x80\x91\x9d.

出于测试目的,您可以将以下文本放入文本文件 1.txt:a and a at the line end 或者您可以使用此文件: https://drive.google.com/file/d/1E-8oZaLb86x0JE_gFpTkeX9jrbh3OXbF/view?usp=sharing

在像 Sublime 这样的编辑器中,我无法使用十六进制代码来匹配这些特殊字符。 不确定是否有其他方法可以做到这一点。

用perl,我也比不上他们。我想使用正则表达式删除所有这些类似汉堡包的字符:

perl -Mutf8::all -pE's,\xf4\x80\x91\x9a,,g; s,\xf4\x80\x91\x9d,,g;' 1.txt > 2.txt

有什么方法可以做到吗?

你能尝试将文件读取为 bytes/binary(使用 :raw IO 层):

use feature qw(say);
use strict;
use warnings;

my $fn = 'test.txt';
open ( my $fh, '<:raw', $fn ) or die "Could not open file '$fn': $!";
my $txt = do { local $/; <$fh> };
close $fh;
my @replace = ("\xf4\x80\x91\x9a", "\xf4\x80\x91\x9d");
my ($pat ) = map {qr/$_/} join "|", map quotemeta, @replace;
$txt =~ s/$pat//g;
print $txt;