我有一个日志文件,如下所示:
'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'
或
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
我正在尝试制作一个正则表达式,以便以"电子邮件IP"格式导出内容。
我尝试使用正则表达式,例如:
([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}(.*)([0-9]{1,3}[.]){3}[0-9]{1,3})
但是当然不起作用,因为这也会获得 2 个匹配字符串之间的全部内容。
如何忽略找到的 2 个字符串之间的内容?
我试图否定正则表达式部分但没有成功。
提前感谢大家!
附言我需要使用 grep 执行此操作
这是我
丑陋的正则表达式解决方案(有效):
([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}`);
});
}
但正如评论中提到的:一个好的 csv 解析器可能会更好!
.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);