[英]c++ regex in loop
The following attempts to get the "a" from "A:a" and "b" from "B:b" and "c" from "C:c" by performing a regex search in a loop.以下尝试通过在循环中执行正则表达式搜索来从“A:a”和“b”从“B:b”和“c”从“C:c”获取“a”。 In the first iteration of the outer loop, "a" appears in the second group of m.
在外循环的第一次迭代中,“a”出现在第二组 m 中。 However for the second iteration of the outer loop, "b" appears in the third group of m.
然而,对于外循环的第二次迭代,“b”出现在 m 的第三组中。 And for the third iteration of the outer loop, "c" appears in the fourth group of m.
对于外循环的第三次迭代,“c”出现在 m 的第四组中。 Why is that?
这是为什么? Also, the first iteration of the outer loop has 4 groups in m.
此外,外循环的第一次迭代在 m 中有 4 个组。 Why is that?
这是为什么? Shouldn't there be two groups?
不应该有两个组吗?
std::string s = "abc\n"
"A:a\n"
"def\n"
"B:b\n"
"ghi\n"
"C:c\n\n"
"jkl\n";
std::regex p("\nA:\\s*([^\n]+)|\nB:\\s*([^\n]+)|\nC:\\s*([^\n]+)");
std::smatch m;
while (std::regex_search(s, m, p))
{
int count = 0;
for (auto x:m)
{
std::cout << count << "->" << x << ", ";
count++;
}
s = m.suffix().str();
}
The output is输出是
0->
A:a, 1->a, 2->, 3->, 0->
B:b, 1->, 2->b, 3->, 0->
C:c, 1->, 2->, 3->c,
Expected:预期的:
0->
A:a, 1->a, 0->
B:b, 1->b, 0->
C:c, 1->c,
Group numbers don't reset in each alternative.组号不会在每个选项中重置。
Since all your alternatives are the same except for the letter at the beginning, you can simply combine them into a single alternative and match the letter with [ABC]
.由于除了开头的字母外,所有替代项都相同,因此您可以简单地将它们组合成一个替代项,并将该字母与
[ABC]
匹配。
std::regex p("\n[ABC]:\\s*([^\n]+)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.