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>