您好,我希望得到以下表達式的值: POLYGON(100 20,30 40,20 10,21 21) 搜索POLYGON(100 20,30 40,20 10 21 21)正則表達式與BoostRegex C++
當我執行下面的代碼我獲得該結果:
POLYGON(100 20,30 40,20 10,21 21)
結果= 100 20
R2 = 100個
R2 = 20個
R2 =,21 21
r2 = 21
大小= 7
我不知道爲什麼我沒有獲得middled值...
感謝您的幫助
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
void testMatch(const boost::regex &ex, const string st) {
cout << "Matching " << st << endl;
if (boost::regex_match(st, ex)) {
cout << " matches" << endl;
}
else {
cout << " doesn’t match" << endl;
}
}
void testSearch(const boost::regex &ex, const string st) {
cout << "Searching " << st << endl;
string::const_iterator start, end;
start = st.begin();
end = st.end();
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
while(boost::regex_search(start, end, what, ex, flags))
{
cout << " " << what.str() << endl;
cout << " result = " << what[1] << endl;
cout << " r2 = " << what[2] << endl;
cout << " r2 = " << what[3] << endl;
cout << " r2 = " << what[4] << endl;
cout << " r2 = " << what[5] << endl;
cout << "size = " << what.size() << endl;
start = what[0].second;
}
}
int main(int argc, char *argv[])
{
static const boost::regex ex("POLYGON\\(((\\-?\\d+) (\\-?\\d+))(\\, (\\-?\\d+) (\\-?\\d+))*\\)");
testSearch(ex, "POLYGON(1 2)");
testSearch(ex, "POLYGON(-1 2, 3 4)");
testSearch(ex, "POLYGON(100 20, 30 40, 20 10, 21 21)");
return 0;
}