【问题标题】:"email ip" regex into log file将“电子邮件 ip”正则表达式写入日志文件
【发布时间】:2017-06-16 22:06:19
【问题描述】:

我有一个看起来像这样的日志文件:

'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})

但当然不起作用,因为这也会获得两个匹配字符串之间的全部内容。

如何忽略两个找到的字符串之间的内容?

我试图否定该正则表达式部分但没有成功。

提前感谢大家!

附:我需要使用 grep 来做到这一点

【问题讨论】:

  • 这看起来像引用的 CSV 那么为什么不使用 CSV 解析器呢?
  • 那是纯文本文件,不是 csv :/
  • CSV 文件是一种纯文本文件,其中包含固定顺序的值,以逗号分隔。不是这样吗?
  • 我的一些旧日志格式不同,没有逗号和顶点,这就是我需要正则表达式的原因
  • 您需要的是 2 的捕获组!你想要的东西(regex1)(regex2) 测试@regex101.com

标签: regex regex-negation regex-lookarounds regex-greedy


【解决方案1】:

这是我丑陋的正则表达式解决方案(有效):

([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}`);
    });
}

但正如 cmets 所述:一个好的 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);

【讨论】:

  • 也许我做错了什么,这适用于 PHP,但如果我需要使用与 grep 相同的正则表达式,则无法再次返回整个匹配项。我启动的示例命令: 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
  • 我将添加 PHP 等效项,但您从未在问题中指定语言!
  • ehm.. 我需要在 grep 中这样做
  • @GiuseppePirlo 啊,好吧,这似乎在纯 grep 中是不可能的。你可以试试 bash!来源:stackoverflow.com/questions/1891797/…
猜你喜欢
  • 1970-01-01
  • 2011-12-28
  • 2012-01-02
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多