2017-04-08 124 views
-1

我想從字符串中提取數字:從串c提取號碼++正則表達式

string line = "00:00:15,000 --> 00:00:18,000"; 

smatch results; 

bool found = regex_match(line, results, regex("(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d) --> (\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)")); 

/*for (auto result : results) { 
    cout << result << endl; 
}*/ 

int res = atoi(*results[3]); 

我可以輕鬆打印結果(註釋代碼),但我不能隱蔽結果爲int。對於這個結果數組,由於某種原因解引用不起作用。

回答

1

在C++ 11中,您應該使用std::stoistd::string轉換爲整數。 (並且不需要解引用它;實際上,*results[3]是編譯錯誤,因爲它不是指針。)

int number = stoi(results[3]); 
cout << number << endl; // 15