Getting parts of a URL (Regex)

后端 未结 26 2127
说谎
说谎 2020-11-22 02:13

Given the URL (single line):
http://test.example.com/dir/subdir/file.html

How can I extract the following parts using regular expressions:

  1. The Subd
26条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 03:05

    Try the following:

    ^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?
    

    It supports HTTP / FTP, subdomains, folders, files etc.

    I found it from a quick google search:

    http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx

提交回复
热议问题