Parsing hostname and port from string or url

前端 未结 5 2079
迷失自我
迷失自我 2021-02-07 00:54

I can be given a string in any of these formats:

  • url: e.g http://www.acme.com:456

  • string: e.g www.acme.com:456, www.acme.com 456, or www.acme.co

5条回答
  •  一向
    一向 (楼主)
    2021-02-07 01:33

    The reason it fails for:

    www.acme.com 456
    

    is because it is not a valid URI. Why don't you just:

    1. Replace the space with a :
    2. Parse the resulting string by using the standard urlparse method

    Try and make use of default functionality as much as possible, especially when it comes to things like parsing well know formats like URI's.

提交回复
热议问题