简体   繁体   English

通过正则表达式替换 URL 中的多个斜线

[英]Multiple slash in URL replacement though regex

I am trying to create a regex in pcre, that is going to salinize URL with multiple slashes like the following:我正在尝试在 pcre 中创建一个正则表达式,它将使用多个斜线对 URL 进行盐化,如下所示:

https://www.domin.com/test1/////test2/somemoretests_67142 https://www.domin.com/test1/test2/somemoretests_67142///// https://www.domin.com/test1/test2///somemoretests_67142

So that I can replace it with the following: https://\2\4 and the link at the end of it looks: https://www.domin.com/test1/test2/somemoretests_67142这样我就可以用以下内容替换它: https://\2\4并且它末尾的链接看起来是: https://www.domin.com/test1/test2/somemoretests_67142

I have been struggling with it for the past couple of days, so any regex guru help is more than welcome:)在过去的几天里,我一直在努力解决这个问题,所以非常欢迎任何正则表达式大师的帮助:)

I have tried the following and more:我尝试了以下以及更多:

(http|https):\/\/(.*)(\/\/+)(.*) (http|https):\/\/(.*)(\/\/){2,}(.*) (http|https):\/\/(.*)(\/\/{2})(.*) (http|https):\/\/(.*)(\/\/+)(.*) (http|https):\/\/(.*)(\/\/){2,}(.*) (http|https):\/\/(.*)(\/\/{2})(.*)

I am going to utilize these for Akamai to sanitize our URLs though cloudlet.我将利用这些让 Akamai 通过 cloudlet 清理我们的 URL。

You can try:你可以试试:

(?<!https:\/)(?<!http:\/)(\/+$|(?<=\/)\/+)

And substitute the first group with empty string.并将第一组替换为空字符串。

Regex demo.正则表达式演示。


This will produce this output:这将产生这个 output:

https://www.domin.com/test1/test2/somemoretests_67142
https://www.domin.com/test1/test2/somemoretests_67142
https://www.domin.com/test1/test2/somemoretests_67142

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM