在 Perl 中,如何解析可能包含许多电子邮件地址的字符串以获取地址列表?
我想拆分包含 ;
或 ,
的字符串。
例如:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当然,您可以使用 split 正如其他人所示。但是,如果
$str
包含完整的电子邮件地址,您将陷入困境。相反,使用 Email::Address:
输出:
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:
Output:
请注意,不能使用双引号来分配
$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:分割(/[.;]/, $str)
split(/[.;]/, $str)
您还可以使用 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.
回答邮件标题中的问题(与正文略有不同):
To answer the question in the title of the mail (a little different from its text):
通过“;”分割或“,”,
其中正则表达式表示匹配转义的 ;或 , 字符。
最终结果将是 @result = ['abc','def','hij']
To split by ";" or ","
Where the regex means to match on an escaped ; or , character.
The end result will be that @result = ['abc','def','hij']