2012-09-28 5 views
2

가능한 복제를 부스트 정규식 일치하는 결과를 변환 :
How to convert a number to string and vice versa in C++C++는 다른 형식으로

나는 아래로 정수와 같은 다른 형식으로 부스트 :: 정규식 일치하는 결과를 변환하는 방법을

암호?

string s = "abc123"; 
boost::regex expr("(\\s+)(\\d+)"); 
boost::smatch match; 
if(boost::regex_search(s, match, expr)) { 
    string text(match[0]); 
    // code to convert match[1] to integer 
} 

답변

2

당신이 match[0]로 대신

string text(match[1]); 
// convert match[2] to integer 

을 가지고 싶습니다 확신은 전체 일치하는 것은 (여기 ABC123), 그래서 submatch 색인 1.

으로 시작입니다 정수 부분으로 변환하려면 lexical_cast을 사용하는 것이 편리합니다.

string s = "abc123"; 
boost::regex expr("(\\s+)(\\d+)"); 
boost::smatch match; 
if(boost::regex_search(s, match, expr)) { 
    string text(match[1]); 
    int num = boost::lexical_cast<int>(match[2]); 
}