我尝试使用多个分隔符(空格和括号)来拆分字符串,但是我只能使用一个分隔符进行拆分,这要归功于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';
}