tom*_*tom 2 regex email indexing perl parsing
我在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!");
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,$ 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.
Run Code Online (Sandbox Code Playgroud)
并且启用strict会阻止它甚至编译:
#!/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.
Run Code Online (Sandbox Code Playgroud)
换句话说,你的问题不是正则表达式工作或不工作,而是你匹配的字符串包含"abcdefgh.com",而不是你所期望的.
| 归档时间: |
|
| 查看次数: |
4066 次 |
| 最近记录: |