Regular expression parsing URL parameters

Source: Internet
Author: User

Parse URL parameter regular: (? <=\?| &) [\w\={},: ' ""]* (? <=[^#])

The project will be used for a long time or not. Finally give up using split split method to resolve the discovery of the outdated

   Public StaticNameValueCollection QueryString (stringpath) {            if(string. IsNullOrEmpty (Path)) {return NULL; }          varArr=path. Split (New Char[] {'?'}, Stringsplitoptions.removeemptyentries); if(arr.) Length! =2)          {              return NULL; }          varValues = arr[1]; Arr= values. Split (New Char[] {'&'}, Stringsplitoptions.removeemptyentries); if(arr = =NULL)return NULL; string[] itemvalues; NameValueCollection Nvcs=NewNameValueCollection (); foreach(varItemincharr) {Itemvalues= Item. Split (New Char[] {'='}, Stringsplitoptions.removeemptyentries); if(Itemvalues = =NULL|| Itemvalues. Length = =0)Continue; Nvcs. ADD (itemvalues[0], itemvalues. Length <=1?string. empty:itemvalues[1]); }          returnNvcs; }

And then go see the regular document. Modified version

  Public StaticNameValueCollection QueryString (stringpath) {            if(string. IsNullOrEmpty (Path)) {return NULL; }            varm = regex.matches (Path,@"(? <=\?| &) [\w\={},: ' ""]* (? <=[^#])", Regexoptions.none); if(M.count <=0)            {                return NULL; } NameValueCollection Nvcs=NewNameValueCollection (); string[] Itemvalues =NULL;  for(inti =0; i < M.count; i++) {itemvalues= M[i]. Value.split (New Char[] {'='}, Stringsplitoptions.removeemptyentries); if(Itemvalues = =NULL|| Itemvalues. Length = =0)Continue; Nvcs. ADD (itemvalues[0], itemvalues. Length <=1?string. empty:itemvalues[1]); }            returnNvcs; }

Regular expression parsing URL parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.