barfoon
barfoon

Reputation: 28157

RegEx - Looking for emails inside of a log file

I am looking for a regular expression that will test for matches against a string such as:

mxtreme1.log:May 12 07:00:00 10.1.1.175 postfix/cleanup[48145]: C2C9FFA730: fullname=, sender=LOGINNAME@company.com, [email protected], [email protected], prior=, as_score=0, as_strategy=M, code=W, actions=FFFFFFFFFFFTFFFFFFFFFF, analysis=F000FFF000TTT000F000TFT000000TTSS3000059900033-F1F-FF00000000F000FFF000000000000F1FFF000F000

where the entire part in bold is a match, but LOGINNAME can be any number of random characters.

Any help would be greatly appreciated, Thank you,

Upvotes: 1

Views: 182

Answers (5)

thisismydisplayname
thisismydisplayname

Reputation: 31

To test all of the above solutions, i personally love using 'The Regex Coach'

Just google for that string and its a freeware that has served me well.

PS: I dont own nor have any sort of vested interest in the product or the team that built it.

Upvotes: 2

Phill Pafford
Phill Pafford

Reputation: 85318

You can try this:

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$

But look at this post to get why RegEx and emails address are problematic.

Also this to add the SENDER=:

^[\w]+=[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$

Upvotes: 0

rampion
rampion

Reputation: 89053

Here's the regex for email addresses according to RFC2822.

It's surprisingly long, but email addresses can be more complex than you may expect.

Just prefix it with /sender\s*=\s*/ to only get sender emails.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338218

That would be something like:

sender=[^@]+@company\.com

(You were explicitly stating that only the LOGINNAME part would be variable.)

Upvotes: 2

Chad Birch
Chad Birch

Reputation: 74558

I'm pretty sure that a comma isn't valid inside of an email address, so as long as it always has the comma afterwards, you should be able to get it with:

/(sender=[^,]+?),/

Upvotes: 1

Related Questions