2011-03-06 2 views
0

안녕하세요 저는 다음 식의 값을 얻고 싶습니다 : POLYGON (100 20, 30 40, 20 10, 21 21) POLYGON (100 20, 30 40, 20 10, 21 21)정규식 BoostRegex C++

다음 코드는 I이 결과 획득 실행할 때 :
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; 
} 

답변

0

나는 IRC 채널로부터 결과를 얻었다. 정규식은 다음과 같습니다.

static const boost::regex ex("[\\d\\s]+"); 
static const boost::regex ex("[\\-\\d\\s]+"); 
2

내가 정규식 전문가,하지만 난 일반을 읽을 수 있어요 표현과 올바른 것 같습니다.

This forum post은 Boost.Regex가 정규 표현식의 마지막 결과 만 리턴하는 것과 정확히 똑같은 것을 말하고있는 것처럼 보입니다. 분명히 기본적으로 Boost는 매치 반복의 마지막 매치만을 추적합니다. 그러나 이것을 변경할 수있는 실험적 기능이 있습니다. More info here의 "반복 캡처"아래에 있습니다.

  1. 당신이 때까지, 다음, 숫자의 첫 번째 쌍을 추적 제거하는 쌍의 문자열을받을 수 있도록 문자열에 다른 정규식을 할 정규식을 사용하여 '

    는 생각이 다른 "솔루션"이있다 모든 정보를 얻었습니다.

  2. Boost.Spirit을 사용하면 입력을 구문 분석하는 데 Boost.Regex보다 더 적합 할 것입니다.