简体   繁体   中英

“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.

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.

How can i ignore the contents between the 2 found strings?

I tried to negate that regex part without success.

Thanks to everyone in advance!

Ps I need do this using 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

 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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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