获取 URL 的一部分(正则表达式) [英] Getting parts of a URL (Regex)
问题描述
给定 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:
- 子域(测试)
- 域名 (example.com)
- 不带文件的路径(/dir/subdir/)
- 文件(file.html)
- 文件路径(/dir/subdir/file.html)
- 不带路径的网址 (http://test.example.com)
- (添加您认为有用的任何其他内容)
即使我输入以下 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屋!