Getting parts of a URL (Regex)

后端 未结 26 2547
说谎
说谎 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条回答
  •  萌比男神i
    2020-11-22 03:06

    subdomain and domain are difficult because the subdomain can have several parts, as can the top level domain, http://sub1.sub2.domain.co.uk/

     the path without the file : http://[^/]+/((?:[^/]+/)*(?:[^/]+$)?)  
     the file : http://[^/]+/(?:[^/]+/)*((?:[^/.]+\.)+[^/.]+)$  
     the path with the file : http://[^/]+/(.*)  
     the URL without the path : (http://[^/]+/)  
    

    (Markdown isn't very friendly to regexes)

提交回复
热议问题