-2

I am trying to extract email address from a txt file. I've thought about surrounding words that contain the '@' character. Does anybody know a expression to do that?

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
Peterstone
  • 7,119
  • 14
  • 41
  • 49
  • 2
    [What have you tried so far?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Alex Reynolds Aug 15 '12 at 08:05
  • maybe you want regex? if yes, then see something like this (see inside [this module](https://metacpan.org/module/Email::Valid)) – gaussblurinc Aug 15 '12 at 08:06
  • @loldop: I want a regex but thats is not what I am looking for. As you can see the description of that module: "This module determines whether an email address is well-formed, and optionally, whether a mail host exists for the domain." What I am looking for is not for check if any email es valid, what I am looking for is just for extracting email addressed from a txt file. – Peterstone Aug 15 '12 at 08:12
  • 2
    Just google "regex mail address"... – mpe Aug 15 '12 at 08:16
  • 1
    mpe, don't send people off to a Web search engine. Link to a Stack Overflow question with good answers instead. – daxim Aug 15 '12 at 12:52

2 Answers2

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";
  }
DVK
  • 126,886
  • 32
  • 213
  • 327
  • Thank you for your post even is the typical question that generates more questions when you try to compilate. See my new question about that: http://stackoverflow.com/questions/11983041/perl-error-message-cant-locate-in-inc – Peterstone Aug 16 '12 at 08:15
2

Here's a very quick and dirty regex which will match non-whitespace characters on either side of an @:

/\S+@\S+/

This will match john.smith@example.com in

some rubbish text john.smith@example.com more rubbish text

Hope this helps.

Denham Coote
  • 634
  • 8
  • 18