C + + regular Gets the parameters in the URL

Source: Internet
Author: User

In the process of accessing the Web page, most of the pages are submitted by get, in order to identify the operation or to access the number of the object. So there are URLs that we often see, like Http://longzhu.com/channels/speed?from=figameindex.

So how do the parameters in the URL get, in ASP. request["from"], if the parameter does not exist or does not have the parameter, then returns NULL, if present, can convert the returned result to the corresponding type, and then handle accordingly.

The author recently studied the regular expressions in c++11, so I want to use the regular in C + + to realize the corresponding function. Paste the code below.

Weburl class definition

1 #ifndef Web_url_h_2 #defineWeb_url_h_3 4#include <regex>5#include <string>6 using namespacestd;7 8 namespaceCrystal {9     classWEBURL {Ten      Public: OneWEBURL (Const string&URL): _url (URL) {} AWEBURL (string&&URL): _url (Move (URL)) {} -  -         stringRequest (Const string& request)Const; the     Private: -         string_url; -     }; - } +  - #endif
View Code

Weburl class implementation

1#include"WebUrl.h"2 3 namespaceCrystal {4     stringWeburl::request (Const string& request)Const {5 smatch result;6         if(Regex_search (_url.cbegin (), _url.cend (), result, regex (Request +"=(.*?) &"))) {7             //to match URLs with multiple parameters8 9             //* Repeat any number of times, but repeat as little as possibleTen             returnresult[1]; One}Else if(Regex_search (_url.cbegin (), _url.cend (), result, regex (Request +"=(.*)"))) { A             //match URLs with only one parameter -  -             returnresult[1]; the}Else { -             //Does not contain parameters or does not exist for formulating parameters -  -             return string(); +         } -     } +}
View Code

Test code

1#include <iostream>2#include"WebUrl.h"3 using namespacestd;4 using namespaceCrystal;5 6 intMain () {7     Try {8WEBURL Web ("Www.123.com/index.aspx?catalog=sport&id=10&rank=20&hello=hello");9cout << web. Request ("Catalog") <<Endl;Tencout << web. Request ("ID") <<Endl; Onecout << web. Request ("Rank") <<Endl; Acout << web. Request ("Hello") <<Endl; -cout << web. Request (" World") <<Endl; -}Catch(Constregex_error&e) { thecout << E.code () <<Endl; -cout << e.what () <<Endl; -     } -  +     return 0; -}
View Code

Reference: http://bbs.csdn.net/topics/320034235

Reprint please specify the source http://www.cnblogs.com/wuhanqing/p/4575690.html

by Crystal

C + + regular Gets the parameters in the URL

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.