std:: string xStr( "AAAA-12222-BBBBB-44455" );
boost::regex xRegEx( "(\\w+)-(\\d+)-(\\w+)-(\\d+)" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
std::cout << "Does this line match our needs? " << std::boolalpha << boost::regex_match(xStr, xResults, xRegEx) << "\n" ;
std::cout << "Print entire match:\n " << xResults[0] << std::endl;
std::cout << "Print the former string into another format:\n" << xResults[1] << "+"
<< xResults[2] << "+"
<< xResults[3] << "+"
<< xResults[4] << std::endl;
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
Does this line match our needs? true
Print entire match:
AAAA-12222-BBBBB-44455
Print the former string into another format:
AAAA + 12222 + BBBBB + 44455
std:: string xStr( "The boost library has a great opportunity for the regex!" );
boost::regex xRegEx( "(\\b\\w{5}\\b)*" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
if ( boost::regex_search(xStr, xResults, xRegEx, boost::match_extra) )
{
std::cout << "Words consist from exact 5 digits have been found in our line:\n" ;
for ( int j = 0; j < xResults.captures(1).size(); ++j)
std::cout << xResults.captures(1)[j] << std::endl;
}
* This source code was highlighted with Source Code Highlighter .
std:: string xStr( "AAAA-12222" );
boost::regex xRegEx( "(\\w+)-(\\d+)-(\\w+)-(\\d+)" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
std::cout << "Does this line match the regex? " << std::boolalpha << boost::regex_match(xStr, xResults, xRegEx,
boost::match_default | boost::match_partial) << "\n" ;
std::cout << "Is it the partial match? " << std::boolalpha << !xResults[0].matched << "\nPrint the partial match:\n" << xResults[0] << std::endl;
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
Does this line match the regex? true
Is it a partial match? true
Print the partial match:
AAAA-12222
std:: string xStr( "The boost library has a great opportunity for the regex!" );
boost::regex xRegEx( "\\b(?:\\w+?)((\\w)\\2)(?:\\w+?)\\b" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
std:: string ::const_iterator xItStart = xStr.begin();
std:: string ::const_iterator xItEnd = xStr.end();
while ( boost::regex_search(xItStart, xItEnd, xResults, xRegEx) )
{
std::cout << "Word, we've searched, is \"" << xResults[0] << "\". It has two \"" << xResults[2] << "\" inside itself.\n" ;
xItStart = xResults[1].second;
}
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
Word, we've searched, is a "boost." It has two "o" inside itself.
Word, we've searched, is “opportunity.” It has two "p" inside itself.
std:: string xStr( "AAAA-12222-BBBBB-44455" );
boost::regex xRegEx( "(\\w+)-(\\d+)-(\\w+)-(\\d+)" );
std:: string xFormatString( "$1*$2*$3*$4" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
std::cout << "Print string after replace:\n " << boost::regex_replace(xStr, xRegEx, xFormatString, boost::match_default | boost::format_perl) << std::endl;
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
Print string after replace:
AAAA * 12222 * BBBBB * 44455
std:: string xStr( "AAAA-12222-BBBBB-44455" );
boost::regex xRegEx( "(\\w|\\d)+" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
boost::sregex_iterator xIt(xStr.begin(), xStr.end(), xRegEx);
boost::sregex_iterator xInvalidIt;
while (xIt != xInvalidIt)
std::cout << *xIt++ << "*" ;
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
AAAA * 12222 * BBBBB * 44455 *
std:: string xStr( "AAAA-12222-BBBBB-44455" );
boost::regex xRegEx( "(\\w|\\d)+" );
boost::smatch xResults;
std::cout << "==========================Results============================== \n" ;
boost::sregex_token_iterator xItFull(xStr.begin(), xStr.end(), xRegEx, 0);
boost::sregex_token_iterator xInvalidIt;
std::cout << "Result the same as the regex_iterator: \n" ;
while (xItFull != xInvalidIt)
std::cout << *xItFull++ << "*" ;
//Parts of captures
boost::regex xRegEx2( "(\\w+)-(\\d+)" );
boost::sregex_token_iterator xItFirstCapture(xStr.begin(), xStr.end(), xRegEx2, 1);
std::cout << "\nShow only first captures: \n" ;
while (xItFirstCapture != xInvalidIt)
std::cout << *xItFirstCapture++ << "*" ;
//Reverse order
int aIndices[] = {2,1};
boost::sregex_token_iterator xItReverseCapture(xStr.begin(), xStr.end(), xRegEx2, aIndices);
std::cout << "\nShow captures in the reverse order: \n" ;
while (xItReverseCapture != xInvalidIt)
std::cout << *xItReverseCapture++ << "*" ;
//Delimiters
boost::regex xRegEx3( "(\\w|\\d)+" );
boost::sregex_token_iterator xItDelimiters(xStr.begin(), xStr.end(), xRegEx3, -1);
std::cout << "\nShow delimiters: \n" ;
while (xItDelimiters != xInvalidIt)
std::cout << *xItDelimiters++ << " " ;
* This source code was highlighted with Source Code Highlighter .
========================== Results ====================== =======
Result the same as the regex_iterator:
AAAA * 12222 * BBBBB * 44455 *
Show only first captures:
AAAA * BBBBB *
Show captures in the reverse order:
12222 * AAAA * 44455 * BBBBB *
Show delimiters:
- - -
Source: https://habr.com/ru/post/64226/