1

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!

P.s. I need do this using grep

1 Answers1

0

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);
online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • Maybe im doing something wrong, that works with PHP but if i need use the same regex with a grep fails returning the whole match again. Example command i launched: grep -E -o "([a-z0-9]+@[a-z0-9.]+).*?([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" a.txt – Giuseppe Pirlo Jan 31 '17 at 13:09
  • I will add the PHP equivalent, but you never specified the language in your question! – online Thomas Jan 31 '17 at 13:10
  • ehm.. well i need do that in grep – Giuseppe Pirlo Jan 31 '17 at 13:12
  • @GiuseppePirlo ah ok, it seems that is not possible in pure grep. You could try bash! source: http://stackoverflow.com/questions/1891797/capturing-groups-from-a-grep-regex#answer-1891890 – online Thomas Jan 31 '17 at 13:14