none
如何用正则表达式提取URL中的参数名称与值? RRS feed

  • 问题

  • string urlStr="pnumber=15012345678&name=!@#¥%……&*()《》?“|}{阿斯蒂芬12345678&&gender=1&relation=1"

    有4个参数及对应的值:
    pnumber=15012345678
    name=!@#¥%……&*()《》?“|}{阿斯蒂芬12345678&
    gender=1
    relation=1

    用正则表达式匹配代码:

    NameValueCollection nameValueCollection =new NameValueCollection ();

    Regex regex = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
    MatchCollection matchCollection = regex.Matches(urlStr);
    foreach (Match match in matchCollection)
    {
         nameValueCollection.Add(match.Result("$2").ToLower(), match.Result("$3"));
    }

    匹配后却是如下结果,其中name的值为什么只提取了部分内容:

    pnumber=15012345678
    name=!@#¥%……
    gender=1
    relation=1

    如何解决?

    2018年5月24日 3:41

答案

  • 可以用URI类

    Use System.URI class

    The Query property of the URI class returns the entire query as a string

    Uri bUri = new Uri("http://somedomain.com/website/webpage.aspx?token=123456&language=English");
    
    var query = bUri.Query.Replace("?", "");

    Now query will have the string value "token=123456&language=English"

    Then use LINQ to produce a Dictionary from the query attributes

    var queryValues = query.Split('&').Select(q => q.Split('='))
                       .ToDictionary(k => k[0], v => v[1]);

    You can then access the values as

    queryValues["token"]

    which will give you 123456


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月25日 0:44
  • .NET已经有内置方法

    string uri = ...; string queryString = new System.Uri(uri).Query; var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月25日 0:46
  • 你好,

    你这样的字符串,没有办法做。我建议的做法是,在传参数的时候把值中含有= 和& 用其他的字符代替,或者先Encode 一下在传值。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月25日 5:32

全部回复

  • 可以用URI类

    Use System.URI class

    The Query property of the URI class returns the entire query as a string

    Uri bUri = new Uri("http://somedomain.com/website/webpage.aspx?token=123456&language=English");
    
    var query = bUri.Query.Replace("?", "");

    Now query will have the string value "token=123456&language=English"

    Then use LINQ to produce a Dictionary from the query attributes

    var queryValues = query.Split('&').Select(q => q.Split('='))
                       .ToDictionary(k => k[0], v => v[1]);

    You can then access the values as

    queryValues["token"]

    which will give you 123456


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月25日 0:44
  • .NET已经有内置方法

    string uri = ...; string queryString = new System.Uri(uri).Query; var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年5月25日 0:46
  • .NET已经有内置方法

    string uri = ...; string queryString = new System.Uri(uri).Query; var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    感谢回复!

    取参数的目的是把各参数值转义后再组合成完成的URL,进行HttpWebRequest 请求。

    Uri类中有个Uri.EscapeUriString()方法可以做到这一点,但是无法对参数值进行完全转义。只能提取各参数值,通过Uri.EscapeDataString()进行转义后再组合成完整的URL。

    这么做太笨了,有其他好方法?

    2018年5月25日 1:31
  • 你好,

    你这样的字符串,没有办法做。我建议的做法是,在传参数的时候把值中含有= 和& 用其他的字符代替,或者先Encode 一下在传值。

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年5月25日 5:32