【发布时间】:2020-12-13 03:59:30
【问题描述】:
类似于Parse comma-separated ints/int-ranges in C++,
我想要一个正则表达式从字符串中提取边缘:(1,2,1) (2,4,5) (1,4,3) (3,4,10) (3,6,2) (3,5,3) (6,7,6) (4,7,4) where (Node1 number, Node2 number, distance)。
我目前正在使用:std::regex reg_edge("\(.*?\,.*?\,.*?\)");,它不起作用(因为找不到一个匹配项)。
由于这也可能是一个 XY 问题,我将说明我想要做什么:我希望用户在创建图形时输入图形的边缘。
请建议一个正确的正则表达式,或者可能是一个更好的方法。
我当前的代码:
void Graph::setEdges() {
std::string edge_str;
std::getline(std::cin, edge_str);
std::istringstream iss(edge_str);
edge_str.clear();
while (iss >> edge_str) {
std::regex reg_edge("\(.*?\,.*?\,.*?\,\)");
auto reg_begin = std::sregex_iterator(edge_str.begin(), edge_str.end(), reg_edge);
auto reg_end = std::sregex_iterator();
for (std::sregex_iterator reg_it = reg_begin; reg_it != reg_end; reg_it++) {
std::smatch it_match = *reg_it;
}
}
}
【问题讨论】:
-
您可以使用简单的
reg_edge(R"(\((\d+),(\d+),(\d+)\))"),然后获取组值reg_it.str(1)、reg_it.str(2)和reg_it.str(3)。好吧,如果您不需要详细的输出,请使用reg_edge(R"(\(\d+(?:,\d+){2}\))") -
对于这样一个简单的解析,我会编写简单的解析代码。检查开头的
(,搜索到下一个,,提取子字符串。等等。
标签: c++ regex string parsing split