(iv) The regular expression of the boost library regex
The regular expression can bring us great convenience, with it, no longer have to worry about this
Header file:
#include <boost/regex.hpp>
1. Exact match
std::stringstr("ABCD");
Boost::reg("A\\w*d");
if (Regex_match (str, REG))
{
std::"is matchStd::endl;
}
Else
{
STD:: "is not matchStd::endl;
}
2. Exactly match and get substring
"[email protected]";
Boost::cmatch Res;
Create 3 sub-expressions
Boost::reg("(\\w+) @(\\w+). (\\w+) ");
if (Boost::regex_match (Mail,res, Reg))
{
Data can be obtained either through iterators or by array
for (Boost::cmatch::iterator pos = Res.begin (); pos! = Res.end (); ++pos)
{
std::Std::endl;
}
RES[0] Store the complete string that matches to
std::"name:" << res[Std::endl;
}
3, find, when you do not need to match the entire string, you can choose to find
"[email protected]";
Boost::cmatch Res;
Create 3 sub-expressions
Boost::reg("(\\w+) @(\\w+). (\\w+) ");
if (Boost::regex_search (Mail,res, Reg))
{
std::cout <<"**************************************Std::endl;
Data can be obtained either through iterators or by array
for (Boost::cmatch::iterator pos = Res.begin (); pos! = Res.end (); ++pos)
{
std::Std::endl;
}
RES[0] Store the complete string that matches to
std::"match:" << res["name:" << res[Std::endl;
}
4. Replace
Replace the substring that matches to, you can reference the nth match to the value by $n, $& reference the full match
#include <boost/algorithm/string.hpp>
Testreplace()
{
Replace [email protected] with [email protected]
std::stringmail("[Email protected]");
Create 3 sub-expressions
Boost::reg("(\\w+) @(\\w+). (\\w+) ");
std::"[email protected]$3std::endl;
std::"[email protected]$2.$3std::endl;
Custom substitution function, regex_replace passing a matching string array to the callback function, returning the new string from the callback function
std::cout << boost::regex_replace (Mail, Reg, [] (const Boost::smatch &m) {
Return boost::to_upper_copy (m[0].str ());
});
}
5. Iteration
When you need to extract multiple expressions from a string, you can use an iteration to extract
std::stringstr("[email protected], [email protected], [email protected]");
Boost::reg("(\\w+) @(\\w+). (\\w+) ");
Boost::pos(Str.begin (), str.end(), Reg);
Boost::sregex_iterator end;
while (pos! = end)
{
std::"[" << (*pos) ["]";
++pos;
}
6. Participle
#include <iostream>
#include <boost/regex.hpp>
Testtoken()
{
Std
namespace boost;
Stringstr("[email protected], [email protected], [email protected]");
Reg("\\w+");
POS(Str.begin (), str.end(), Reg);
while (pos! = Sregex_token_iterator ())
{
"["]" ;
++pos;
}
cout << Endl;
If the last parameter, args, is-1, the matched string is treated as a delimiter
Split_reg(",");
pos = Sregex_token_iterator (Str.begin (), Str.end (), Split_reg,-1);
while (pos! = Sregex_token_iterator ())
{
"["]" ;
++pos;
}
cout << Endl;
If the last parameter, args, is a positive number, the first args substring of the matching result is returned
Split_sub_reg("(\\w*) @(\\w*). (\\w*) ");
1);
while (pos! = Sregex_token_iterator ())
{
"["]" ;
++pos;
}
cout << Endl;
Match and specify output order
Extract the date from the following string and convert it to the sequential output of month and day
std::stringinput("01/02/2003 blahblah 04/23/1999 blahblah 11/13/1981");
Re("(\\d{2})/(\\d{2})/(\\d{4})date
Year,month, Day
Begin(Input.begin (), input. End(), Re, sub_matches), end;
To Std::cout
std::std::"\ n");
Std::copy (begin, end, Out_iter);
}
(iv) The regular expression of the boost library regex