以下Perl脚本和正则表达式从给定文本文件中提取所有电子邮件地址。
示例文本:
This perl script extracts all email addresses from an given text file. This email address is valid: web@email.net and this email address is not valid web@email. Same as what_ever@public.com is a valid email address and address test@test. is not valid !
使用Perl通过正则表达式提取电子邮件地址:
#!/usr/bin/perl use strict; my $email_count; while (my $line = <>) { #read from file or STDIN foreach my $email (split /\s+/, $line) { if ( $email =~ /^[-\w.]+@([a-z0-9][a-z-0-9]+\.)+[a-z]{2,4}$/i ) { print $email . "\n"; $email_count++; } } } print "Emails Extracted: $email_count\n";
执行:
$ ./extract_email.pl emails.txt web@email.net what_ever@public.com emails Extracted: 2
日期:2020-07-07 20:54:34 来源:oir作者:oir