문제

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.example.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])?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top