2017-04-08 60 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