-2

我正在尝试从 txt 文件中提取电子邮件地址。我考虑过包含“@”字符的周围单词。有人知道这样做的表达式吗?

4

2 回答 2

4

Whenever you need some reasonably common matching problem resolve in Perl, you should always first check the Regexp::Common family on CPAN. In this case: Regexp::Common::Email::Address. From POD Synopsys:

  use Regexp::Common qw[Email::Address];
  use Email::Address;

  while (<>) {
      my (@found) = /($RE{Email}{Address})/g;
      my (@addrs) = map $_->address, Email::Address->parse("@found");
      print "X-Addresses: ", join(", ", @addrs), "\n";
  }
于 2012-08-15T08:50:08.160 回答
2

这是一个非常快速和肮脏的正则表达式,它将匹配 两侧的非空白字符@

/\S+@\S+/

这将匹配john.smith@example.com

一些垃圾文字 john.smith@example.com 更多垃圾文字

希望这可以帮助。

于 2012-08-15T08:13:04.113 回答