正则表达式跳过第一个词并解析消息的其余部分

Regex to skip first word and parse the rest of the message

我一直在尝试获取正确的正则表达式来跳过第一个单词并解析消息的其余部分。

I've been testing the regex by running Logstash locally

grok {
    match => { "resource" => "/[^/]+/[^/]+(/|)(?<repo>[^/]+)?(/%{GREEDYDATA:resource_path})?" }
      }

Test Messages:

/list/Lighter-test-group/xyz/123
/list/
/list

For messages,

/list/Lighter-test-group/xyz/123 gives us repo value as "Lighter-test-group" which is valid
/list/ gives us repo value as null which is valid
but /list gives repo value as "list" which is an invalid value. The correct value needs to be empty or null.

不确定您是否只能使用一个非常长的正则表达式,但我会研究 custom patterns 以忽略第一个词。

使用这个 grok debugger,我在第三个框中设置了一些自定义模式:

IGNORE /\b\w+\b
REPO [A-Za-z]([A-Za-z0-9+\-.]+)+

并在第二个框中测试了这个 grok 模式:

%{IGNORE}(/)?(%{REPO:repo})?(%{GREEDYDATA:resource_path})

使用这些自定义模式,我能够得到我认为是你想要的输出,但如果你有的话,我会用更多的用例来测试它们。