考虑代码:
regex boundary{ "\\s*\\b\\s*" };
string test = "foo bar\t baz-floop";
auto begin = sregex_token_iterator(test.begin(), test.end(), boundary, -1);
for (auto i = begin; i != sregex_token_iterator{}; i++) {
cout << *i << endl;
}
代码改编自other answer并打算通过正则表达式拆分字符串。调用它的结果(在 VC++ 16.2.3 上)是:
oo
ar
az
loop
如何更正代码,以便不删除匹配项的第一个字母?我无法更改正则表达式本身。此外,根据我的解释,Java 中的类似代码似乎可以工作:
Pattern boundary = Pattern.compile("\\s*\\b\\s*");
String test = "foo bar\t baz-floop";
String[] results = boundary.split(test);
for (String result : results) {
System.out.println(result);
}
最佳答案
这是标准库中的错误。固定here .
关于c++ - 通过正则表达式拆分字符串会删除结果的第一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57909110/