开发者

Matching until the first forward slash not followed by a forward slash [duplicate]

开发者 https://www.devze.com 2023-01-10 09:39 出处:网络
This question already has answers here: 开发者_JS百科 Regex: match first slash in url -- thats it!! ? not working for some reason?
This question already has answers here: 开发者_JS百科 Regex: match first slash in url -- thats it!! ? not working for some reason? (2 answers) Closed 2 years ago.

I have URLs in the following format:

STATIC_PATH=http://abc.com/0123/3456
STATIC_PATH=http://xyz.com

I want to match until and including the first forward slash not immediately followed by a forward slash. In the first URL that would match be http://abc.com/, in the second URL, it would be http://xyz.com. Can you give me the regex for it? Thank you.


[^/]*(/(/[^/]*/?)?)?

match everything up to the first backslash, then match that backslash (if it exists), a second immediately following backslash (if it exists) and then everything up to the third backslash.


In your favourite language, do a few splits on the string. eg python

>>> url="STATIC_PATH=http://abc.com/0123/3456"
>>> url.split("=")
['STATIC_PATH', 'http://abc.com/0123/3456']
>>> url.split("=")[1]
'http://abc.com/0123/3456'
>>> url.split("=")[1].split("/")
['http:', '', 'abc.com', '0123', '3456']
>>> url.split("=")[1].split("/")[0:3]
['http:', '', 'abc.com']
>>> '/'.join( url.split("=")[1].split("/")[0:3] )
'http://abc.com'


I did figure this out. I am doing negative lookback and negative lookahead. ^.?((?| [^M]$)

0

精彩评论

暂无评论...
验证码 换一张
取 消