How to get the domain and string to the first slash

For example like this

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

I can get the with this.

However I want to get the (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 = ''
    url = url.split('//')
    result = url[1].split('/')[:2]
    result = '/'.join(result)


    OR USING re

    Bro, I am not good at the re module.

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


    <re.Match object; span=(0, 24), match=''>

  • answered 2022-05-07 06:14 Avinash

    Try this regex pattern:


    import re
    url = ""
    result ='(?:\w*://)(?P<host>\w*\.\w*/\w*)', url)


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