首页 > 解决方案 > Splunk 正则表达式以匹配部分 url 字符串

问题描述

我正在尝试使用 Splunk 搜索特定 url 的所有基本路径实例(然后可能将其绘制在图表上)。

以下是一些示例网址和我想要匹配的部分:

http://some-url.com/first/  # match "first"
http://some-url.com/first/second/ # match "first"
http://some-url.com/first/second/third/  # match "first"

这是我正在使用的正则表达式,效果很好:

http:\/\/some-url\.com\/(.*?)\/

我的 Splunk 搜索应该是什么来提取所需的文本?这在 Splunk 中是否可行?

标签: regexsplunk

解决方案


要匹配任何 URL(.com 与否),您可以使用以下命令。

index=... | rex field=_raw "http(s)?://[^/]+/(?<match>[^/]+)"

这将匹配诸如

http://splunk.com/first/
https://simonduff.net/first/
https://splunk.com/first/middle/last
https://splunk.com/first

推荐阅读