regex - 需要编写一个正则表达式来提取前 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 个或最多达到一个数值。
解决方案
这种丑陋会做你想做的事(尽管你可能需要去掉结尾/
,如果你不想要它(注意 - 它在路径之前跳过 URL 中的所有内容):
\/\/[^\/]+(?<pathnoendingnumbers>\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+(\/[a-zA-Z-_][-_\w]+)?)?)?)?)[\/\d$]?
这是一场 88 步的比赛,这可能是可以改进的——但它的效率很高
推荐阅读
- tensorflow - “时代”在训练生成对抗网络中意味着什么
- html - coreui-如何更改模板的样式?
- mysql - 如果还有以下内容,如何使用 SQL CONCAT 连接列并在它们之间包含“&”
- flutter - Flutter、Dart 和 FirebaseAuth:如何将标志链接到用户?
- vue.js - WebRTC Vuejs,但视频不显示
- java - 如何在android studio中显示应用程序设计窗口?
- python - 如何使用 jupyterlab notebook 启动和安装?
- bash - stderr 未正确定向
- node.js - 如何在 Node 脚本的 js 中使用 .ejs 变量?
- entity-framework - Automapper Project 无法使用结构创建地图表达式