解析URL路径

时间:2018-05-30 01:40:29

标签: regex

我正在尝试解析URL中的路径,例如:

http://www.website.com/first-path/second-path/third-path

我有以下正则表达式:[^/\s][^/]*[^/\s]*

我需要单独提取first-pathsecond-paththird-path,只需要忽略http://www.website.com/

编辑:可以有零个或n个路径。我也可以使用捕获组。

编辑2:使用http://(.*?)/(?!.*\/$)(.*),我可以隔离第二组,但需要隔离每条路径

编辑3:当只有两条路径时,使用http://(.*?)/(.*?)(?:/(.*?)/(.*?))?$失败:

http://www.example.com/first-path/second-path/third-path
http://www.example.com/first-path
http://www.example.com/first-path/second-path

1 个答案:

答案 0 :(得分:0)

尝试正则表达式:(?<=\G\/|\.com\/)([\w-]+)

Demo

相关问题