获取 URL 的一部分(正则表达式) [英] Getting parts of a URL (Regex)

查看:44
本文介绍了获取 URL 的一部分(正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 URL(单行):
http://test.example.com/dir/subdir/file.html

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

如何使用正则表达式提取以下部分:

How can I extract the following parts using regular expressions:

  1. 子域(测试)
  2. 域名 (example.com)
  3. 不带文件的路径(/dir/subdir/)
  4. 文件(file.html)
  5. 文件路径(/dir/subdir/file.html)
  6. 不带路径的网址 (http://test.example.com)
  7. (添加您认为有用的任何其他内容)

即使我输入以下 URL,正则表达式也应该可以正常工作:

The regex should work correctly even if I enter the following URL:

http://example.example.com/example/example/example.html

推荐答案

一个正则表达式来解析和分解一个包含查询参数的完整 URL和锚点,例如

A single regex to parse and breakup a full URL including query parameters and anchors e.g.

https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash

^((http[s]?|ftp):/)?/?([^:/s]+)((/w+)*/)([w-.]+[^#?s]+)(.*)?(#[w-]+)?$

RexEx 职位:

url: RegExp['$&'],

url: RegExp['$&'],

protocol:RegExp.$2,

protocol:RegExp.$2,

host:RegExp.$3,

host:RegExp.$3,

path:RegExp.$4,

path:RegExp.$4,

file:RegExp.$6,

file:RegExp.$6,

query:RegExp.$7,

query:RegExp.$7,

hash:RegExp.$8

hash:RegExp.$8

然后你可以很容易地进一步解析主机('.'分隔).

you could then further parse the host ('.' delimited) quite easily.

会做的是使用这样的东西:

What I would do is use something like this:

/*
    ^(.*:)//([A-Za-z0-9-.]+)(:[0-9]+)?(.*)$
*/
proto $1
host $2
port $3
the-rest $4

进一步解析其余"以尽可能具体.用一个正则表达式来做,嗯,有点疯狂.

the further parse 'the rest' to be as specific as possible. Doing it in one regex is, well, a bit crazy.

这篇关于获取 URL 的一部分(正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆