解析web url并具有键值对
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

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

#0

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

#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];
}

#2

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.

推荐文章

防止内存碎片

防止内存碎片

推荐文章

如何在ASP.NET MVC中服务具有“异常”文件扩展名的内容文件

如何在ASP.NET MVC中服务具有“异常”文件扩展名的内容文件

推荐文章

多行ASP.NET文本框的正则表达式

多行ASP.NET文本框的正则表达式

推荐文章

为什么可以在函数参数中指定数组的大小?

为什么可以在函数参数中指定数组的大小?

推荐文章

一个简单的Facebook对话框示例,其中包含一个将参数发布回rails控制器的表单

一个简单的Facebook对话框示例,其中包含一个将参数发布回rails控制器的表单

推荐文章

为什么不能直接引用可以导入的命名空间?

为什么不能直接引用可以导入的命名空间?

推荐文章

引发异常或返回值

引发异常或返回值

推荐文章

Windows服务中无法识别System.Windows.Forms命名空间

Windows服务中无法识别System.Windows.Forms命名空间

推荐文章

在模型中调用ActionText是不是不好?

在模型中调用ActionText是不是不好?

推荐文章

有什么可以从buzz中获取评论的吗?

有什么可以从buzz中获取评论的吗?

推荐文章

Sqlite查询优化(按限制偏移量从where in和not order中选择*)

Sqlite查询优化(按限制偏移量从where in和not order中选择*)

推荐文章

不区分大小写数组唯一

不区分大小写数组唯一

推荐文章

在表值函数和视图中重用表定义

在表值函数和视图中重用表定义

推荐文章

NHibernate-将一个属性映射到两个或多个字段?

NHibernate-将一个属性映射到两个或多个字段?

推荐文章

如何在iphone上存储持久的对象数组?

如何在iphone上存储持久的对象数组?

推荐文章

如何将内部查询的结果转换为字符串?

如何将内部查询的结果转换为字符串?