简体   繁体   English

RegEx-在日志文件中查找电子邮件

[英]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 , from=LOGINNAME@company.com, recip=LOGINNAME@company.com, prior=, as_score=0, as_strategy=M, code=W, actions=FFFFFFFFFFFTFFFFFFFFFF, analysis=F000FFF000TTT000F000TFT000000TTSS3000059900033-F1F-FF00000000F000FFF000000000000F1FFF000F000 mxtreme1.log:5月12日07:00:00 10.1.1.175 postfix / cleanup [48145]:C2C9FFA730:fullname =, 发件人= LOGINNAME @ company.com ,来自= LOGINNAME @ company.com ,recip = LOGINNAME @ company.com ,优先级=,as_score = 0,as_strategy = M,代码= W,操作= FFFFFFFFFFFTFFFFFFFFFF,分析= F000FFF000TTT000F000TFT000000TTSS3000059900033-F1F-FF00000000F000FFF000000000000F1FFF000F000

where the entire part in bold is a match, but LOGINNAME can be any number of random characters. 其中整个粗体部分都是匹配项,但LOGINNAME可以是任意数量的随机字符。

Any help would be greatly appreciated, Thank you, 任何帮助将不胜感激,谢谢,

That would be something like: 就像这样:

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

(You were explicitly stating that only the LOGINNAME part would be variable.) (您明确地指出,只有LOGINNAME部分是可变的。)

To test all of the above solutions, i personally love using 'The Regex Coach' 为了测试以上所有解决方案,我个人非常喜欢使用“ Regex教练”

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. PS:我对产品或制造它的团队没有所有权,也没有任何既得利益。

我很确定在电子邮件地址中逗号不是有效的,因此只要后面总是逗号,您应该可以通过以下方式获得它:

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

Here's the regex for email addresses according to RFC2822 . 这是根据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. 只需在其前面加上/sender\\s*=\\s*/即可获得发件人的电子邮件。

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. 但是请看这篇文章,以了解为什么RegEx和电子邮件地址有问题。

Also this to add the SENDER=: 这也是添加SENDER =:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM