Home > Back-end >  How to get the domain and string to the first slash
How to get the domain and string to the first slash

Time:05-07

For example like this

 url = https://example.com/test/myname/myname.jpg
 result = re.search('(?:https?://)?(?P<host>.*?)(?:[:#?/@]|$)', url)

I can get the example.com with this.

However I want to get the example.com/test (string before first /)

how can I do this?

CodePudding user response:

Instead of re you can easily by splitting the list.

url = 'https://example.com/test/myname/myname.jpg'
url = url.split('//')

result = url[1].split('/')[:2]

result = '/'.join(result)
print(result)

OUTPUT:

example.com/test

OR USING re

Bro, I am not good at the re module.

import re
url = 'https://example.com/test/myname/myname.jpg'

result = re.search('(?:https?://)?(?P<host>.*?)(?:[:#?/@]|$).[^/] ', url)
print(result)

OUTPUT

<re.Match object; span=(0, 24), match='https://example.com/test'>

CodePudding user response:

Try this regex pattern:

(?:\w*://)(?P<host>\w*\.\w*/\w*)

https://regex101.com/r/wotbTN/1

import re

url = "https://example.com/test/myname/myname.jpg"
result = re.search('(?:\w*://)(?P<host>\w*\.\w*/\w*)', url)

print(result["host"])

Output:

example.com/test
  • Related