在 Perl 中,如何解析可能包含许多电子邮件地址的字符串以获取地址列表?

发布于 2024-09-29 22:23:27 字数 1137 浏览 6 评论 0原文

我想拆分包含 ;, 的字符串。

例如:

$str = "[email protected];[email protected],[email protected];[email protected];";

预期结果是:

result[0]="[email protected]";
result[1]="[email protected]";
result[2]="[email protected]";
result[3]="[email protected]";

I want to split the a string if it contains ; or ,.

For example:

$str = "[email protected];[email protected],[email protected];[email protected];";

The expected result is:

result[0]="[email protected]";
result[1]="[email protected]";
result[2]="[email protected]";
result[3]="[email protected]";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

花间憩 2024-10-06 22:23:27

当然,您可以使用 split 正如其他人所示。但是,如果 $str 包含完整的电子邮件地址,您将陷入困境。

相反,使用 Email::Address

#!/usr/bin/perl

use strict; use warnings;
use Email::Address;
use YAML;

print Dump [ map [$_->name, $_->address ],
    Email::Address->parse(
        q{[email protected];"Tester, Test" <[email protected]>,[email protected];[email protected]}
    )
];

输出:

---
-
  - a
  - [email protected]
-
  - 'Tester, Test'
  - [email protected]
-
  - c
  - [email protected]
-
  - d
  - [email protected]

Sure, you can use split as shown by others. However, if $str contains full blown email addresses, you will be in a world of hurt.

Instead, use Email::Address:

#!/usr/bin/perl

use strict; use warnings;
use Email::Address;
use YAML;

print Dump [ map [$_->name, $_->address ],
    Email::Address->parse(
        q{[email protected];"Tester, Test" <[email protected]>,[email protected];[email protected]}
    )
];

Output:

---
-
  - a
  - [email protected]
-
  - 'Tester, Test'
  - [email protected]
-
  - c
  - [email protected]
-
  - d
  - [email protected]
末骤雨初歇 2024-10-06 22:23:27
my $str = '[email protected];[email protected],[email protected];[email protected];';
my @result = split /[,;]/, $str;

请注意,不能使用双引号来分配 $str,因为 @ 很特殊。这就是为什么我用单引号替换字符串分隔符。你也可以像这样逃避它们:

my $str = "a\@a.com;b\@b.com,c\@c.com;d\@d.com;";
my $str = '[email protected];[email protected],[email protected];[email protected];';
my @result = split /[,;]/, $str;

Note that you can't use double-quotes to assign $str because @ is special. That's why I replaced the string delimiters with a single-quote. You could also escape them like so:

my $str = "a\@a.com;b\@b.com,c\@c.com;d\@d.com;";
小兔几 2024-10-06 22:23:27

分割(/[.;]/, $str)

split(/[.;]/, $str)

梦过后 2024-10-06 22:23:27

您还可以使用 Text::Csv 并使用“;”或“,”用于分割。它也有助于查看其他内容,例如可打印字符等。

You could also use Text::Csv and use either ";" or "," for splitting. It helps to look at other things like printable characters etc as well.

绾颜 2024-10-06 22:23:27

回答邮件标题中的问题(与正文略有不同):

my $str = 'abc@xyz;qwe@rty;';

my @addrs = ($str =~ m/(\w+\@[\w\.]+)/g);

print join("<->", @addrs);

To answer the question in the title of the mail (a little different from its text):

my $str = 'abc@xyz;qwe@rty;';

my @addrs = ($str =~ m/(\w+\@[\w\.]+)/g);

print join("<->", @addrs);
淡淡的优雅 2024-10-06 22:23:27

通过“;”分割或“,”,

$test = "abc;def,hij";
@result = split(/[;,]/, $test);

其中正则表达式表示匹配转义的 ;或 , 字符。
最终结果将是 @result = ['abc','def','hij']

To split by ";" or ","

$test = "abc;def,hij";
@result = split(/[;,]/, $test);

Where the regex means to match on an escaped ; or , character.
The end result will be that @result = ['abc','def','hij']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文