Getting parts of a URL (Regex)

后端 未结 26 2540
说谎
说谎 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 02:51

    regexp to get the URL path without the file.

    url = 'http://domain/dir1/dir2/somefile' url.scan(/^(http://[^/]+)((?:/[^/]+)+(?=/))?/?(?:[^/]+)?$/i).to_s

    It can be useful for adding a relative path to this url.

提交回复
热议问题