解析网址并获得键值对

发布于 2024-12-28 06:08:24 字数 552 浏览 2 评论 0原文

我想解析以下网址。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶气 2025-01-04 06:08:25

如果您可以控制生成 URL 的代码,那么我建议您更改它以在键值对之间引入分隔符。就目前情况而言,很难区分值的结束位置和下一个键的开始位置。

这是一步一步的解决方案。

首先,从您的 URL 中提取查询字符串(我假设它位于 urlString 中):

Uri uri = new Uri(urlString);
NameValueCollection outer = HttpUtility.ParseQueryString(uri.Query);

由于其编码方式,您的查询字符串在技术上仅包含一个键值对,其中键为k。其余的键值对都在 k 的值内进行编码。因此,我们将获取该值并对其进行解码:

string k = outer["k"];
string decoded = HttpUtility.UrlDecode(k);

下一部分是最棘手的:提取您的键和值。我们将使用正则表达式来匹配这些:

var matches = Regex.Matches(decoded, @"(?<key>\w+):(?<value>.*?)(?= \w+:|$)");

我们将假设您的键仅由单词字符(字母、数字和下划线)组成;因此,它们将通过 (?\w+) 进行匹配。每个键后面都有一个 : 字符。接下来,值可以由任意字符序列(?.*?)组成。但是,它们后面必须跟有另一个键 \w+: 或字符串结尾 $;因此,我们将使用正向前瞻来匹配其中任何一个。

最后,只需将所有匹配项转换为字典即可:

IDictionary<string, string> dictionary = matches.Cast<Match>().ToDictionary(
    m => m.Groups["key"].Value, 
    m => m.Groups["value"].Value);

结合起来,这将为您提供:

Uri uri = new Uri(urlString);
NameValueCollection outer = HttpUtility.ParseQueryString(uri.Query);
string k = outer["k"];
string decoded = HttpUtility.UrlDecode(k);
var matches = Regex.Matches(decoded, @"(?<key>\w+):(?<value>.*?)(?= \w+:|$)");
IDictionary<string, string> dictionary = matches.Cast<Match>().ToDictionary(
    m => m.Groups["key"].Value, 
    m => m.Groups["value"].Value);

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):

Uri uri = new Uri(urlString);
NameValueCollection outer = HttpUtility.ParseQueryString(uri.Query);

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 for k. Thus, we shall get that value and decode it:

string k = outer["k"];
string decoded = HttpUtility.UrlDecode(k);

The next part is the trickiest: extracting your keys and values. We shall use regex for matching these:

var matches = Regex.Matches(decoded, @"(?<key>\w+):(?<value>.*?)(?= \w+:|$)");

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:

IDictionary<string, string> dictionary = matches.Cast<Match>().ToDictionary(
    m => m.Groups["key"].Value, 
    m => m.Groups["value"].Value);

Combined, this would give you:

Uri uri = new Uri(urlString);
NameValueCollection outer = HttpUtility.ParseQueryString(uri.Query);
string k = outer["k"];
string decoded = HttpUtility.UrlDecode(k);
var matches = Regex.Matches(decoded, @"(?<key>\w+):(?<value>.*?)(?= \w+:|$)");
IDictionary<string, string> dictionary = matches.Cast<Match>().ToDictionary(
    m => m.Groups["key"].Value, 
    m => m.Groups["value"].Value);
盗琴音 2025-01-04 06:08:25

一个 JS 解决方案,我会更概括一些(例如,正确获取 k url 参数值)

var url = 'http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company';
var args = unescape(url).split('?k=')[1];
args = args.split(/\s+(?=\w+:)/);

var results = {};
for (i in args) {
  keyval = args[i].split(':');
  results[keyval[0]] = keyval[1];
}

A JS solution, I would generalize it a bit more (for example, getting the k url param value properly)

var url = 'http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company';
var args = unescape(url).split('?k=')[1];
args = args.split(/\s+(?=\w+:)/);

var results = {};
for (i in args) {
  keyval = args[i].split(':');
  results[keyval[0]] = keyval[1];
}
薄暮涼年 2025-01-04 06:08:25

这是 C# 的开始:

string MyUrl = "http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company";

MyUrl = HttpUtility.UrlDecode(MyUrl);

之后您应该能够使用 String.Split。

Here's a start in C#:

string MyUrl = "http://testurl?k=firstname%3AA%20department%3AIT%20Development%20Company%3ATest%20Company";

MyUrl = HttpUtility.UrlDecode(MyUrl);

You should be able to use String.Split after that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文