gpt4 book ai didi

regex - 使用 Perl 正则表达式删除引号和撇号的所有变体

转载 作者:行者123 更新时间:2023-12-01 03:41:16 25 4
gpt4 key购买 nike

我正在尝试从字符串中删除撇号和双引号,并注意到有各种版本会根据创建方式创建到我正在使用的数据中。例如,Word 文档倾向于使用这些:

It’s raining again. 
What do you mean by “weird”?

而文本编辑器是这样的:
It's raining again.
What do you mean by "weird"?

当我浏览各种字符图表和数据时,我注意到引号和撇号还有其他变体,例如: http://www.fileformat.info/info/unicode/char/0022/index.htm

虽然我可以通过并找到所有合理的工作,但是否有现有的 Perl 正则表达式或函数可以删除引号和撇号的所有变体?

最佳答案

为了删除所有引号和撇号,您可以使用

 [\p{Pi}\p{Pf}'"]

并替换为空字符串。

demo

IDEONE demo :
#!/usr/bin/perl
use utf8;
my $st = "“Quotes1” «Quotes2» ‘Quotes3’ 'Quotes4' \"Quotes5\"";
print "Before: $st\n";
$st =~ s/[\p{Pi}\p{Pf}'"]//g;
print "After: $st\n";

“说”
Before: “Quotes1” «Quotes2» ‘Quotes3’ 'Quotes4' "Quotes5"
After: Quotes1 Quotes2 Quotes3 Quotes4 Quotes5

关于regex - 使用 Perl 正则表达式删除引号和撇号的所有变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30620307/

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