Data validation of a subexpression (subexpressions), which decomposes the subexpression of a regular expression by means of parentheses "()";
Then use the subscript designator [], output the corresponding subexpression, "0" on behalf of the whole, "1" for the first bracket, followed by accumulator;
Verify the match of the regular expression by matching the question mark item?
Code:
#include <iostream> #include <string> #include <boost/regex.hpp> using Namespa
CE std;
using namespace boost; BOOL Valid (const boost::smatch& m) {if (m[1].matched) return m[3].matched && (m[4].matched = = 0 | |
M[4].str () = = "");
else return!m[3].matched && m[4].str () = = M[6].str (); int main () {//question mark (?) Before you can choose std::string phone = "(\)? ( \\D{3}) (\))? ([-. ])? (\\d{3}) ([-. ]?)
(\\d{4}) ";
Boost::regex R (phone);
Boost::smatch m;
std::string s ("(432) 312-3425 9424151424 15");
For (Boost::sregex_iterator It (S.begin (), S.end (), R), End_it; It!= end_it; ++it) {if (valid (*it)) std::cout << "valid:" << it->str () << std::end
L
else std::cout << "not valid:" << it->str () << Std::endl; }
}
Output:
Valid: (432) 312-3425
valid:9424151424
Author: csdn Blog spike_king
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/