Regex 正则表达式匹配URL,不带尾随斜杠,也不带文件扩展名
到目前为止,我读了这么多关于正则表达式的文章,对此我感到非常困惑 我希望匹配第一个URL,其余URL不应匹配:Regex 正则表达式匹配URL,不带尾随斜杠,也不带文件扩展名,regex,Regex,到目前为止,我读了这么多关于正则表达式的文章,对此我感到非常困惑 我希望匹配第一个URL,其余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://su
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://subdomain.example.com/test 使用
看
解释
--------------------------------------------------------------------------------
^ 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,还可以反转url并使用:
^\w+\/
^
仅在开头(在本例中为结尾)
\w+
至少有一个字符的任何字母数字字符集
\/
以匹配斜杠
在python中类似于这样:
re.search(r'^\w+\/',url[::-1])
如果这不是None
,则url
的结尾类似于:../someword
注意:只有当您确定url
确实是一个url时,才可以这样做。所有这些答案都应该是这样的。谢谢你。@LearnerforLife完全同意,看到这样的答案真是太好了!
^\w+\/