首页 > 解决方案 > 需要编写一个正则表达式来提取前 5 个斜杠的路径或最多一个 Splunk 的数字

问题描述

嗨,我需要编写一个正则表达式来从路径的前 5 个斜杠或最多一个数字中提取路径示例:

https://example.com/first/second/third/fourth/fifth/sixth 
https://example.com/first-1/second-1/third-1/
https://example.com/first-1/second-1/third-1
https://example.com/first/12345

结果:

/first/second/third/fourth/fifth
/first-1/second-1/third-1
/first-1/second-1/third-1
/first

我可以使用正则表达式剥离域

http(s)*\:\/\/([^\/]+)\/(?<uri>[^\?\s]+)

但是,我无法获得前 5 个或最多达到一个数值。

标签: regexsplunksplunk-query

解决方案


这种丑陋会做你想做的事(尽管你可能需要去掉结尾/,如果你不想要它(注意 - 它在路径之前跳过 URL 中的所有内容):

\/\/[^\/]+(?<pathnoendingnumbers>\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+)?)?)?)?)[\/\d$]?

这是一场 88 步的比赛,这可能是可以改进的——但它的效率很高


推荐阅读