0
我想用下面的代碼解析位置文件,但是我得到一個奇怪的regex_error,當我調用.what()函數時,它簡單地給出了代碼5的「regex_error」,我似乎無法找到問題。爲什麼我的描述少了regex_error?
代碼:
std::string line;
std::ifstream loc_file(argv[1]);
std::regex line_regex(R"(\S+)\s+([0-9\.]+) ([NS])\s+([0-9\.]+) ([EW])");
while (std::getline(loc_file, line)) {
std::smatch m;
std::regex_search(line, m, line_regex);
std::cout << "Location Matches:" << m.length() << std::endl;
std::cout << "Loc:" << m[1];
std::cout << " Lat:" << (m[3] == "S") ? -std::stod(m[2]) : std::stod(m[2]);
std::cout << " Lon:" << (m[5] == "W") ? -std::stod(m[4]) : std::stod(m[4]) << endl;
}
文件格式:
Loc1 0.67408 N 23.47297 E
Loc2 3.S 23.42157 W
OtherPlace 3.64530 S 17.47136 W
SecondPlace 26.13222 N 3.63386 E
我開發我的正則表達式上regex101.com可以test out my regex there
此外,如果它的事項我使用VS2015
你的原始字符串字面需要括號:'R「(<字符串中的位置>)」' – Galik
@Galik奏效,但爲什麼有必要嗎?我可以在哪裏找到關於該文檔的文檔? –
[字符串文字](http://en.cppreference.com/w/cpp/language/string_literal)#(6) –