gpt4 book ai didi

regex - Perl RegEx 查找@之前的电子邮件地址部分

转载 作者:行者123 更新时间:2023-12-02 06:44:02 25 4
gpt4 key购买 nike

我在 Perl 中有以下问题。我有一个文件,我在其中获取电子邮件列表作为输入。

我想解析所有电子邮件地址的“@”之前的字符串。 (后面我会把@之前的字符串全部存到一个数组中)

例如。在:abcdefgh@gmail.com,我想解析电子邮件地址并提取 abcdefgh。

我的意图是只获取“@”之前的字符串。现在的问题是如何使用正则表达式检查它。或者还有其他使用 substr 的方法吗?

虽然我在 Perl 中使用正则表达式:$mail =~ "\@",但它没有给我结果。

另外,我如何找到字符“@”在字符串 $mail 的哪个索引中?

如果有人能帮助我,我将不胜感激。

#!usr/bin/perl

$mail = "abcdefgh@gmail.com";

if ($mail =~ "\@" ) {
print("my name = You got it!");
}
else
{
print("my name = Try again!");
}

在上面的代码中,$mail =~ "\@"没有给我想要的输出,但是 ($mail =~ "abc") 可以。

$mail =~ "@"仅在给定字符串 $mail = "abcdefgh\@gmail.com"时有效;

但在我的例子中,我将获得带有电子邮件地址的输入。

不带转义字符。

谢谢,

汤姆

最佳答案

启用警告会指出您的问题:

#!/usr/bin/perl
use warnings;

$mail = "abcdefgh@gmail.com";
__END__
Possible unintended interpolation of @gmail in string at - line 3.
Name "main::gmail" used only once: possible typo at - line 3.

并且启用严格会阻止它甚至编译:

#!/usr/bin/perl
use strict;
use warnings;

my $mail = "abcdefgh@gmail.com";
__END__
Possible unintended interpolation of @gmail in string at - line 4.
Global symbol "@gmail" requires explicit package name at - line 4.
Execution of - aborted due to compilation errors.

换句话说,您的问题不是正则表达式工作或不工作,而是您匹配的字符串包含“abcdefgh.com”,而不是您的预期。

关于regex - Perl RegEx 查找@之前的电子邮件地址部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3335861/

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