开发者

Using Regexp to get information in a KeyValuePair

Help me to parse this message:

text=&direction=re&orfo=rus&files_id=&message=48l16qL2&old_charset=utf-8&template_id=&HTMLMessage=1&draft_msg=&re_msg=&fwd_msg=&RealName=0&To=john+%3Cjohn11%40gmail.com%3E&CC=&BCC=&Subject=TestSubject&Body=%3Cp%3EHello+%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D1%82%D0%B5%D0%BA%D1%81%D1%82%3Cbr%3E%3Cbr%3E%3C%2Fp%3E&secur

I would like to get information in an KeyValuePair:

开发者_运维问答Key - Value

text -

direction - re

and so on.

And how to convert this: Hello+%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82+%D1%82%D0%B5%D0%BA%D1%81%... there are cyrillic character.

Thanks.


If you want to use a Regex, you can do it like this:

// I only added the first 3 keys, but the others are basically the same
Regex r = new Regex(@"text=(?<text>.*)&direction=(?<direction>.*)&orfo=(?<orfo>.*)");
Match m = r.Match(inputText);

if(m.Success)
{
    var text = m.Groups["text"].Value; // result is ""
    var direction = m.Groups["direction"].Value; // re
    var orfo = m.Groups["orfo"].Value;
}

However, the method suggested by BoltClock is much better:

System.Collections.Specialized.NameValueCollection collection = 
    System.Web.HttpUtility.ParseQueryString(inputString);


It looks like you are dealing with a URI, better to use the proper class than try and figure out the detailed processing.

http://msdn.microsoft.com/en-us/library/system.uri.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜