64,643
社区成员
发帖
与我相关
我的任务
分享
#include <boost/regex.hpp>
#include <iostream>
void print_captures(const std::string& regx, const std::string& text)
{ //boost::regex e("(\\d{3, 4})[- ]?(\\d{4)})[- ]?(\\d{4})[- ]?(\\d{4})");
boost::regex e(regx);
boost::smatch what;
std::cout << "Expression: \"" << regx << "\"\n";
std::cout << "Text: \"" << text << "\"\n";
if (boost::regex_match(text, what, e, boost::match_extra))
{
unsigned i, j;
std::cout << "** Match found **\n Sub-Expressions: \n";
for (i = 0; i < what.size(); ++i)
std::cout << " $" << i << " = \"" << what[i] << "\"\n";
std::cout << " Captures:\n";
}
else
{
std::cout << "** No match found **\n";
}
}
int main(int , char* [])
{
print_captures("(([[:lower:]]+)|([[:upper:]]+))+", "aBBcccDDDDeeeeeee");
return 0;
}
Expression: "(([[:lower:]]+)|([[:upper:]]+))+"
Text: "aBBcccDDDDeeeeeee"
** Match found **
Sub-Expressions:
$0 = "aBBcccDDDDeeeeeee"
$1 = "eeeeeee"
$2 = "eeeeeee"
$3 = "DDDD"
Captures:
Press any key to continue
boost::regex e("(\\d{3, 4})[- ]?(\\d{4)})[- ]?(\\d{4})[- ]?(\\d{4})");
//你的这个正则式有问题:括号不匹配
//所以在构造这个e的时候异常