简体   繁体   English

“email ip”正则表达式进入日志文件

[英]“email ip” regex into log file

I have a logs file looking like: 我有一个日志文件看起来像:

'User_001','Entered server','email@aol.com','2','','','0','YES','0','0',','0','192.168.1.1','192.168.1.2','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1'
'User_002','Entered server','email@aol.com','2','','','0','NO','0','0',','0','192.168.1.3','192.168.1.4','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1'

OR 要么

User_001 Entered server email@aol.com 2 Pool_1 YES 0 0 0 192.168.1.1 192.168.1.2 0 0 0 0 0 0 0 0 0 1 0 0 1
User_002 Entered server email@aol.com 2 Pool_1 NO 0 0 0 192.168.1.3 192.168.1.4 0 0 0 0 0 0 0 0 0 1 0 0 1

And i'm trying to make a regex for export in "Email IP" format the contents. 而我正试图以“电子邮件IP”格式制作出口正则表达的内容。

I tried with a regex like: 我尝试使用正则表达式:

([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}(.*)([0-9]{1,3}[\.]){3}[0-9]{1,3})

But of course doesn't work since that get also the whole content between the 2 matched strings. 但是当然不起作用,因为它也获得了2个匹配字符串之间的全部内容。

How can i ignore the contents between the 2 found strings? 如何忽略2个找到的字符串之间的内容?

I tried to negate that regex part without success. 我试图否定正则表达式部分没有成功。

Thanks to everyone in advance! 感谢大家提前!

Ps I need do this using grep 我需要用grep做这个

This is my ugly regex solution (that works): 这是我丑陋的正则表达式解决方案(有效):

([a-z0-9]+@[a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})

https://www.regex101.com/r/APfJS1/1 https://www.regex101.com/r/APfJS1/1

 const regex = /([a-z0-9]+@[a-z0-9.]+).*?([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})/gi; const str = `User_001','Entered server','email@aol.com','2','','','0','YES','0','0',','0','192.168.1.1','192.168.1.2','0','0','0','0','0','0','0','0','0','1','0','','0','0','0','1'`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } 

But as mentioned in the comments: a good csv parser will be better probably! 但正如评论中所提到的:一个好的csv解析器可能会更好!

PHP PHP

$re = '/([a-z0-9]+@[a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/i';
$str = 'User_001\',\'Entered server\',\'email@aol.com\',\'2\',\'\',\'\',\'0\',\'YES\',\'0\',\'0\',\',\'0\',\'192.168.1.1\',\'192.168.1.2\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'0\',\'1\',\'0\',\'\',\'0\',\'0\',\'0\',\'1\'';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

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

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