使用正则表达式递归匹配列

时间:2014-08-01 15:04:05

标签: regex

我的日志文件格式如下:

<134> 1 2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ]

方括号内的模式可以在一行中重复2次或更多次,因此我的日志文件可能如下所示:

2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ]
2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.127 bablabla - anotherIP 56390 anotherPortNumber - anotherIP anotherPortNumber ]
2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.127 bablabla - anotherIP 56390 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.128 bablabla - anotherIP 56391 anotherPortNumber - anotherIP anotherPortNumber ]

对于每行中的方括号,我需要使用正则表达式匹配第一个IP号和第一个端口号。

我的问题:是否有任何RegEx表示可以递归地从重复模式中提取列(在这种情况下,列将是3和7,其中空格作为分隔符)?

我事先欣赏任何建议

3 个答案:

答案 0 :(得分:1)

这是匹配所有ip和端口的正则表达式

<强>正则表达式

((?:\d+\.){3}\d+)(?:.*?)(\d+)

测试字符串

2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ]
2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.127 bablabla - anotherIP 56390 anotherPortNumber - anotherIP anotherPortNumber ]
2014 Jul 31 18:48:03 - - MYAPP - [blablabla aNumber 10.70.4.126 bablabla - anotherIP 56389 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.127 bablabla - anotherIP 56390 anotherPortNumber - anotherIP anotherPortNumber ][blablabla aNumber 10.70.4.128 bablabla - anotherIP 56391 anotherPortNumber - anotherIP anotherPortNumber ]

<强>结果

MATCH 1

  1. [52-63] 10.70.4.126

  2. [85-90] 56389

  3. MATCH 2

    1. [193-204] 10.70.4.126

    2. [226-231] 56389

    3. MATCH 3

      1. [300-311] 10.70.4.127

      2. [333-338] 56390

      3. MATCH 4

        1. [441-452] 10.70.4.126

        2. [474-479] 56389

        3. MATCH 5

          1. [548-559] 10.70.4.127

          2. [581-586] 56390

          3. MATCH 6

            1. [655-666] 10.70.4.128

            2. [688-693] 56391

            3. 尝试demo here

答案 1 :(得分:1)

我不完全确定我是否明白你需要什么,但在这里我发布这个答案来提取ip和端口。使用这个正则表达式:

\[.*?([\d\.]+).*?\-.*?(\d+).*?\]

<强> Working demo

enter image description here

答案 2 :(得分:0)

这应该这样做:

/(\[([^ ]+ ){2}([^ ])+ ([^ ]+ ){3}([^ ]+ )[^\]]+\])+/