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]$)
精彩评论