gpt4 book ai didi

regex - 无法在 Perl 中使用正则表达式匹配印度卢比货币符号

转载 作者:行者123 更新时间:2023-12-01 10:37:59 26 4
gpt4 key购买 nike

以下是我的文字:

Total: ₹ 131.84

Thanks for choosing Uber, Pradeep

我想匹配金额部分,使用以下代码:

if ( $mail_body =~ /Total: \x{20B9} (\d+)/ ) {
$amount = $1;
}

但是,它不匹配,尝试使用正则表达式调试,这是输出:

Compiling REx "Total: \x{20B9} (\d+)"
Final program:
1: EXACT <Total: \x{20b9} > (5)
5: OPEN1 (7)
7: PLUS (9)
8: DIGIT (0)
9: CLOSE1 (11)
11: END (0)
anchored utf8 "Total: %x{20b9} " at 0 (checking anchored) minlen 10
Matching REx "Total: \x{20B9} (\d+)" against "Total: %342%202%271%302%240131.84%n%nThanks for choosing Ube"...
UTF-8 pattern...
Match failed
Freeing REx: "Total: \x{20B9} (\d+)"

完整代码位于 http://pastebin.com/TGdFX7hg .

最佳答案

免责声明:这感觉更像是评论而不是答案,但我需要更多空间。

我从未使用过MIME::Parser和之前的 friend ,但根据我在文档中阅读的内容,以下内容可能有效:

use Encode qw(decode);

# according to your code, $text_mail is a MIME::Entity object
my $charset = $text_mail->head->mime_attr('content-type.charset');
my $mail_body_raw = $text_mail->bodyhandle->as_string;

my $mail_body = decode $charset, $mail_body_raw;

想法是从 MIME::Head 对象中获取字符集,然后使用 Encode相应地解码正文。

当然,如果您知道它始终是 UTF-8 文本,您也可以对其进行硬编码:

my $mail_body = decode 'UTF-8', $mail_body_raw;

在那之后,你的正则表达式可能仍然无法工作,因为根据你问题中的调试输出,₹和数字之间的字符实际上不是一个简单的空格(ASCII 32,U+0020),而是一个不间断的空格(U+00A0)。您应该能够将其与 \s 相匹配:

if ( $mail_body =~ /Total: \x{20B9}\s(\d+)/ ) {

关于regex - 无法在 Perl 中使用正则表达式匹配印度卢比货币符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501596/

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