<分区>
我尝试用多个定界符(空格和括号)拆分字符串,但由于 getline(...)
,我只能用一个定界符拆分。
这是我尝试做的一个例子:
hello world(12)
我想将论文作为字符串获取:
hello
world
(
12
)
有什么帮助吗?
<分区>
我尝试用多个定界符(空格和括号)拆分字符串,但由于 getline(...)
,我只能用一个定界符拆分。
这是我尝试做的一个例子:
hello world(12)
我想将论文作为字符串获取:
hello
world
(
12
)
有什么帮助吗?
最佳答案
您可以简单地进行匹配。使用下面的正则表达式,然后在必要时将匹配的结果附加到列表中。
[^()\s]+(?=[()])|[^\s()]+|[()]
代码:
#include <regex>
#include <iostream>
int main()
{
std::string s("hello world(12)");
std::regex r("[^()\\s]+(?=[()])|[^\\s()]+|[()]");
auto it = std::sregex_iterator(s.begin(), s.end(), r);
auto end = std::sregex_iterator();
for ( ; it != end; ++it)
std::cout << it->str() << '\n';
}
关于c++ - 如何使用 STL 拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28561850/