How to get the domain and string to the first slash

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?

2 answers

  • answered 2022-05-07 05:59 codester_09

    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'>
    

  • answered 2022-05-07 06:14 Avinash

    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
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum