解析网址并获得键值对
我想解析以下网址。
http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company
http://testurl?k=department%3AIT%20Development%20firstname%3AA%20Company%3ATest%20Company
%3A = ':'
%20 = ' '
http://testurl?k=firstname:A department:IT Development Company:Test Company
http://testurl?k=department:IT Development firstname:A Company:Test Company
我想要的是具有如下所示的键值对。
名字 = A,部门 = IT 开发,公司 = 测试公司
部门 = IT 开发,名字 = A,公司 = 测试公司
谢谢, 阿什什
I wan to parse following URL.
http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company
http://testurl?k=department%3AIT%20Development%20firstname%3AA%20Company%3ATest%20Company
%3A = ':'
%20 = ' '
http://testurl?k=firstname:A department:IT Development Company:Test Company
http://testurl?k=department:IT Development firstname:A Company:Test Company
What I want is to have key value pair as shown below.
FirstName = A, Department = IT Development, Company = Test Company
Department = IT Development, FirstName = A, Company = Test Company
Thanks,
Ashish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以控制生成 URL 的代码,那么我建议您更改它以在键值对之间引入分隔符。就目前情况而言,很难区分值的结束位置和下一个键的开始位置。
这是一步一步的解决方案。
首先,从您的 URL 中提取查询字符串(我假设它位于
urlString
中):由于其编码方式,您的查询字符串在技术上仅包含一个键值对,其中键为
k
。其余的键值对都在k
的值内进行编码。因此,我们将获取该值并对其进行解码:下一部分是最棘手的:提取您的键和值。我们将使用正则表达式来匹配这些:
我们将假设您的键仅由单词字符(字母、数字和下划线)组成;因此,它们将通过
(?\w+)
进行匹配。每个键后面都有一个:
字符。接下来,值可以由任意字符序列(?.*?)
组成。但是,它们后面必须跟有另一个键\w+:
或字符串结尾$
;因此,我们将使用正向前瞻来匹配其中任何一个。最后,只需将所有匹配项转换为字典即可:
结合起来,这将为您提供:
If you have control over the code generating your URLs, then I would recommend that you alter it to introduce a delimiter between your key–value pairs. As it stands, it is hard to distinguish where a value ends and the next key starts.
Here is the solution step-by-step.
First, extract the query string from your URL (which I will assume to reside in
urlString
):Due to the way it is encoded, your query string technically only contains a single key–value pair, with the key being
k
. The rest of your key–value pairs are encoded within the value fork
. Thus, we shall get that value and decode it:The next part is the trickiest: extracting your keys and values. We shall use regex for matching these:
We shall assume that your keys consist only of word characters (letters, digits, and underscores); thus, they would be matched by
(?<key>\w+)
. Each key is followed by a:
character. Next, the values may consist of any sequence of characters,(?<value>.*?)
. However, they must be followed either by another key,\w+:
, or the end of the string,$
; thus, we will use a positive lookahead for matching either of these.Finally, just convert any matches to a dictionary:
Combined, this would give you:
一个 JS 解决方案,我会更概括一些(例如,正确获取
k
url 参数值)A JS solution, I would generalize it a bit more (for example, getting the
k
url param value properly)这是 C# 的开始:
之后您应该能够使用 String.Split。
Here's a start in C#:
You should be able to use String.Split after that.