2010-01-26 5 views
0
boost::regex re; 
    re = "(\\d+)"; 
    boost::cmatch matches; 
    if (boost::regex_search("hello 123 world", matches, re)) 
    { 
    printf("Found %s\n", matches[1]); 
    } 

결과 : "Found 123 world". 방금 "123"을 원했어. 이것은 null-termination에 대한 문제인가 아니면 regex_search가 어떻게 작동하는지 오해하고 있습니까?부스트 정규 표현식 - 검색 결과의 null 종결

답변

2

당신은 그런 식으로는 printf matches[1] (유형 sub_match<T>의 객체)를 전달할 수 없습니다. printf가 char 포인터를 기대하기 때문에 어떤 유용한 결과를 제공한다는 사실은 여러분이 의지 할 수없는 무언가입니다. 당신은 matches[1].str()를 사용하여 결과를 가진 표준 : : 문자열 개체를 얻을 수

printf("Found %s\n", matches[1].str().c_str()); 

: 당신의 printf를 사용하려면

cout << "Found " << matches[1] << endl; 

또는 대신 사용합니다.