我试图从字符串中删除撇号和双引号,并注意到根据其创建方式,我正在使用的数据中创建了各种版本。例如,Word 文档倾向于使用这些:
\n\nIt\xe2\x80\x99s raining again. \nWhat do you mean by \xe2\x80\x9cweird\xe2\x80\x9d?\nRun Code Online (Sandbox Code Playgroud)\n\n而文本编辑器是这样的:
\n\nIt\'s raining again.\nWhat do you mean by "weird"?\nRun Code Online (Sandbox Code Playgroud)\n\n当我浏览各种字符图表和数据时,我注意到引号和撇号还有其他变体,例如: http: //www.fileformat.info/info/unicode/char/0022/index.htm
\n\n虽然我可以通过并合理地找到它们,但是否有现有的 Perl 正则表达式或函数可以删除引号和撇号的所有变体?
\n为了删除所有引号和撇号,您可以使用
\n\n [\\p{Pi}\\p{Pf}\'"]\nRun Code Online (Sandbox Code Playgroud)\n\n并替换为空字符串。
\n\n查看演示
\n\n\n\n#!/usr/bin/perl\nuse utf8;\nmy $st = "\xe2\x80\x9cQuotes1\xe2\x80\x9d \xc2\xabQuotes2\xc2\xbb \xe2\x80\x98Quotes3\xe2\x80\x99 \'Quotes4\' \\"Quotes5\\"";\nprint "Before: $st\\n";\n$st =~ s/[\\p{Pi}\\p{Pf}\'"]//g;\nprint "After: $st\\n";\nRun Code Online (Sandbox Code Playgroud)\n\n《说》
\n\nBefore: \xe2\x80\x9cQuotes1\xe2\x80\x9d \xc2\xabQuotes2\xc2\xbb \xe2\x80\x98Quotes3\xe2\x80\x99 \'Quotes4\' "Quotes5"\nAfter: Quotes1 Quotes2 Quotes3 Quotes4 Quotes5\nRun Code Online (Sandbox Code Playgroud)\n