数据:(\n 只是为了理解而隐含)
Name :John van\n
\n
Email Address :john@abc.com\n
\n
基于正则表达式:
use Data::Dumper;
my @data = m/Name\s*:([A-Za-z\s]*)\n\nEmail Address\s*:([A-Za-z\s]*@[A-Za-z\s]*.[A-Za-z]*)\n/g;
print Dumper @data;
会给
$VAR = [
John van,
john@abc.com
]
如果你想基于行来做,我的方法是:(未经测试 - 尖锐):)
my @data = (
'Name :john van',
'',
'Email Address :john@abc.com',
''
);
my (@persons, $name, $email);
my $gotName = 0;
my $gotEmail = 0;
while(@data) { # data is your read in filehandle
if (/^Name/) {
$name = $_;
$name =~ s/.*://;
chomp($name);
$gotName++;
}
if (/^Email/) {
$mail= $_;
$mail=~ s/.*://;
chomp($mail);
$gotEmail++;
}
if ($gotName == 1 and $gotEmail == 1) {
push(@persons, ($name,$email));
$gotName = 0;
$gotEmail = 0;
}
}
有没有更好的方法来解析文件以获取电子邮件地址和名称?
更好的方法是哪个?
我如何处理字符串并重新排列它们。
问题是什么?