Boost is a complement to STL, a regex is one of the modules. There are a lot of methods, this article records the common methods.
Introducing Header Files <boost/regex.hpp>
1. Regex_match
Regex reg ("\\d{3}");
String str = "123";
BOOL B = Regex_match (Str,reg);
2.regex_replace (string s, regex e, string t) replaces the substring in s that matches E with T
REGEX REG (Colo) (U) (r), Boost::regex::icase|boost::regex::p erl);
String s= "Colour,colour,color,colourize";
S=regex_replace (S,reg, "$1$3");
The $n in T represents the contents of the nth brackets in Reg, and the $ r,$1 represents Colo. The previous code indicates that replace colour with Color,boost::regex::icase/boost::regex::p Erl is a flag switch that ignores case. You can turn on the required logo switch and turn it off by default when you don't need it.
Regex_replace does not modify the original string, but instead generates a new string to return .
3.erase_all_regex (String, regex), (Boost::algorithm::erase_all_regex,in header <boost/algorithm/string/ regex.hpp>), deletes all substrings that satisfy the regex, and it is directly modified in the original string
#include <boost/algorithm/string/regex.hpp>
erase_all_regex (str, Boost::regex ("[\n|\t|\r]")
Delete all spaces in the string str
4.split_regex (Sequential container, string, regex), (<boost/algorithm/string/regex.hpp>), delimiter in regex format, split string, storing results in container
#include <boost/algorithm/string/regex.hpp>
vector<string> fields;
Split_regex (fields, str, Boost::regex ("[\\*| X]));
If str = "5*6", the fields is stored in 5 and 6. STR will not be modified.
5.split (Sequential container, string,predicate), (<boost/algorithm/string/split.hpp>).
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
vector<string> result;
Split (result, School_code, is_any_of (";"));
Is_any_of, which is used to determine whether the school_code contains ";", to divide school_code into result, and not to modify the original string.