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; - }; - } + - #endifView 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