gpt4 book ai didi

用于匹配没有尾部斜杠的 URL 的正则表达式也没有文件扩展名

转载 作者:行者123 更新时间:2023-12-04 08:47:11 25 4
gpt4 key购买 nike

到目前为止,阅读了如此多的正则表达式文章,我感到非常困惑。

我要匹配第一个 URL,其余的应该不匹配:

https://subdomain.example.com/test <== only this should match
https://subdomain.example.com/paht/test.css
https://subdomain.example.com/path/path/test.js
https://example.com/test/

我希望只匹配没有尾部斜杠或文件扩展名的路由。

这是我的正则表达式:https:.*^(?!([^\/]|(\.[a-z]{2,8})))$

您可以在这里尝试:https://regexr.com/5dic8

最佳答案

使用

^https?:\/\/(?:.*\/)?[^\/.]+$

参见 proof

解释

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
s? 's' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
[^\/.]+ any character except: '\/', '.' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

关于用于匹配没有尾部斜杠的 URL 的正则表达式也没有文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64252012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com