// Example program
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string strr("1.0.0.0029.443");
std::regex rgx("([0-9])");
std::smatch match;
if (std::regex_search(strr, match, rgx)) {
for(int i=0;i<match.size();i++)
std::cout << match[i] << std::endl;
}
}
这个程序应该写:
1
0
0
0
0
2
9
4
4
3
但是它会写入。
1
1
我在http://cpp.sh/和Visual Studio上都测试了一下,结果相同。
为什么只找到两个匹配项?并且它们为什么相同?
从这里的答案中我理解到,正则表达式搜索会在第一个匹配项处停止,并且匹配变量保存必要的(子?)字符串值以便继续(通过重复)查找其他匹配项。另外,由于它在第一个匹配项处停止,所以圆括号
()
只用于结果中的子匹配项。
\\d
is a slightly shorter alternative to[0-9]
- AndyG