如何替换除西​​班牙语字符以外的所有 unicode 字符? [英] How to replace all unicode characters except for Spanish ones?

查看:39
本文介绍了如何替换除西​​班牙语字符以外的所有 unicode 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件中删除除西班牙语字符之外的所有 Unicode 字符.

I am trying to remove all Unicode characters from a file except for the Spanish characters.

匹配不同的元音没有任何问题,并且 áéíóúÁÉÍÓÚ 不会使用以下正则表达式替换(但所有其他 Unicode 似乎都被替换了):

Matching the different vowels has not been any issue and áéíóúÁÉÍÓÚ are not replaced using the following regex (but all other Unicode appears to be replaced):

perl -pe 's/[^áéíóúÁÉÍÓÚ[:ascii:]]//g;'文件名

但是当我将倒置的问号 ¿ 或感叹号 ¡ 添加到正则表达式时,其他 Unicode 字符也被匹配并排除了我想被删除的:

But when I add the inverted question mark ¿ or exclamation mark ¡ to the regex other Unicode characters are also being matched and excluded that I would like to be removed:

perl -pe 's/[^áéíóúÁÉÍÓÚ¡¿[:ascii:]]//g;'filename 不会替换以下内容(有些不可打印):³

perl -pe 's/[^áéíóúÁÉÍÓÚ¡¿[:ascii:]]//g;' filename does not replace the following (some are not printable): ³ � � ­

我在这里遗漏了什么明显的东西吗?我也愿意接受在终端上执行此操作的其他方式.

Am I missing something obvious here? I am also open to other ways of doing this on the terminal.

推荐答案

您有一个 UTF8 编码的文件并使用 Unicode 字符,因此,您需要传递一组特定的选项来让 Perl 知道这一点.

You have a UTF8 encoded file and work with Unicode chars, thus, you need to pass specific set of options to let Perl know of that.

您应该添加 -Mutf8 以让 Perl 识别直接在您的 Perl 代码中使用的 UTF8 编码字符.

You should add -Mutf8 to let Perl recognize the UTF8-encoded characters used directly in your Perl code.

此外,您需要传递 -CSD(相当于 -CIOED),以便对您的输入进行解码并重新编码输出.此值取决于编码,适用于 UTF8 编码.

Also, you need to pass -CSD (equivalent to -CIOED) in order to have your input decoded and output re-encoded. This value is encoding dependent, it will work for UTF8 encoding.

perl -CSD -Mutf8 -pe 's/[^áéíóúñüÁÉÍÓÚÑÜ¡¿[:ascii:]]//g;' filename

不要忘记 Üü.

这篇关于如何替换除西​​班牙语字符以外的所有 unicode 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆