#include <boost/regex.hpp>

Source: Internet
Author: User

The regular expression library Boost.regex for boost C + + can apply regular expressions to C + +. Regular expressions greatly reduce the burden of searching for a particular pattern string, which is a powerful feature in many languages.

The two most important classes in the Boost.regex library are Boost::regex and boost::smatch, which are defined in the Boost/regex.hpp file. The former is used to define a regular expression, while the latter can save the search results.

Summary: The regular expression library for C + + has long been available, but no library has ever been incorporated into the standardized process. At present, the library has successfully established a new generation of C + + standard library, the end of C + + no standard regular expression support of the era.

1 Boost::regex_match

Regular expression Matching

2 Boost::regex_replace

Regular expression substitution

3 Boost::regex_search

Regular expression Retrieval

1#include <iostream>2#include <boost/regex.hpp>3 4 voidMain ()5 {6STD::stringstr ="Chinaen 8Glish";7 8Boost::regex Expr ("(\\w+) \\s (\\w+)");9 Ten     //+ used to indicate repetition one or more times One     //d-digit any number between 0-9 A     //S-space any space character -     //U-upper A-Z capital letter. If a region is set, it may contain other characters -     //W-word Any word character-alphanumeric underline the  -Std::cout << boost::regex_match (str, expr) << Std::endl;//match 1, mismatch 0 -  - Boost::smatch what; +  -     if(Boost::regex_search (str, what, expr))//Regular Expression Retrieval +     { AStd::cout << what[0] <<Std::endl; atStd::cout << what[1] <<Std::endl; -Std::cout << what[2] <<Std::endl; -Std::cout << what[3] <<Std::endl; -     } -     Else -     { inStd::cout <<"Retrieval Failed"<<Std::endl; -     } to}

Boost::regex_replace

Regular expression substitution

S-space any space character

1#include <iostream>2#include <boost/regex.hpp>3 4 voidMain ()5 {6STD::stringstr ="Chinaen 8Glish";7 8Boost::regex Expr ("\\s");//S-space any space character9 TenSTD::stringTihuan ="____"; One  AStd::cout << boost::regex_replace (str, expr, Tihuan) << Std::endl;//Replace the expr -}

#include <boost/regex.hpp>

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.