In Perl, how can I extract email addresses from lines in log files?
perl, regex
Solution
#!/usr/bin/env perl
use strict; use warnings;
use Email::Address;
while (my $line = <DATA>) {
my ($addr) = Email::Address->parse($line);
print $addr->address, "\n";
}
__DATA__
00:00:50,004 ERROR [SynchronousCallback] Cannot process resource: test.test@domain.com Channel: channel16
Output:
C:\temp> tt
test.test@domain.com
Problem
I am looking to trim a string that will be created from reading in a file line by line. However i want to pull out only the email from the string, but it will change every time. The only contant is the domain, for example `@domain.com`. So for the input string of ``` 00:00:50,004 ERROR [SynchronousCallback] Cannot process resource: test.test@domain.com Channel: channel16 ``` What regular expression will look for `@domain.com` and pull out all `test.test@domain.com`. Ive got a regex that will look for the string `m/@domain.com/i` but i dont know how to then manipulate the string once the `@domain.com` has been located in the whole string. The output i would like would be just the email `test.test@domain.com`