开发者

perl regexp for mail address

开发者 https://www.devze.com 2023-03-05 08:49 出处:网络
ive got some trouble with writting regex for this lines in exim log 1. 2011-05-12 11:30:26 1QKRHt-0001aD-Vd => mail <mail@mail.example.com> F=<root@example.com> bla bla

ive got some trouble with writting regex for this lines in exim log

 1. 2011-05-12 11:30:26 1QKRHt-0001aD-Vd => mail <mail@mail.example.com> F=<root@example.com> bla bla 
 2. 2011-04-22 12:01:31 1QDCF0-0002ss-Nw => /var/mail/mail <root@mail.mealstrom.org.ua> F=<root@example.com> bla bla 
 3. 2011-05-12 11:29:01 1QKRGU-0001a5-Ok => mail@mail.exampl开发者_开发技巧e.com F=<root@example.com> bla bla

and i want to put to variable this mail@mail.example.com in one regexp. ive tryed to use logic lile this: find last string before 'F=', seperated by whitespaces and can be locked in < >

Can you help me to write this logic?


You can use the following regex:

# the line should be in variable $line
if ($line =~ /.*?\s+<?(\S+?)>?\s+F=/) {
  # ...
}

And then it is a good idea to validate your match with Mail-RFC822-Address perl module, so the full code could be:

use Mail::RFC822::Address qw(valid);

# the line should be in variable $line
if ($line =~ /.*?\s+<?(\S+?)>?\s+F=/) {
  if (valid($1)) {
    # ...
  }
}


Use:

/(?<=<)\S*(?=>\s*F=)/

The (?<= xxx ) syntax is a lookbehind assertion, and the (?= xxx ) is a lookahead assertion.

this will not check the validity of the e-mail address, just extract that part of the line.


Regex is not the measure, Email::Valid is.


Here is a Email Validation Regex

\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b

It will extract an email from anywhere.

I hope this RFC2822 one posts correctly.

[a-z0-9!#$%&'*+/=?^_\`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)\*@(?:\[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+\[a-z0-9](?:[a-z0-9-]\*[a-z0-9])?
0

精彩评论

暂无评论...
验证码 换一张
取 消