Data validation of a subexpression (subexpressions) of a regular expression in C + +

Source: Internet
Author: User
Tags expression include regular expression valid

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/

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.