C++增强:分割字符串
如何使用正则表达式使用 Boost 拆分字符串并将分隔符包含在结果列表中?
例如,如果我有字符串“1d2”并且我的正则表达式是“[az]”,我希望结果在带有 (1, d, 2) 的向量中,
我有:
std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
expression.end (),
re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults));
谢谢
How can I split a string with Boost with a regex AND have the delimiter included in the result list?
for example, if I have the string "1d2" and my regex is "[a-z]" I want the results in a vector with (1, d, 2)
I have:
std::string expression = "1d2";
boost::regex re("[a-z]");
boost::sregex_token_iterator i (expression.begin (),
expression.end (),
re);
boost::sregex_token_iterator j;
std::vector <std::string> splitResults;
std::copy (i, j, std::back_inserter (splitResults));
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能直接使用 boost::regex 提取分隔符。但是,您可以提取字符串中找到正则表达式的位置:
此示例将显示:
使用此信息,您可以从原始字符串中提取分隔符:
此示例将显示:
b 和 c 之间有一个空字符串,因为没有分隔符。
I think you cannot directly extract the delimiters using boost::regex. You can, however, extract the position where the regex is found in your string:
This example would show:
Using this information, you can extract the delimitiers from your original string:
This example would show:
There is an empty string betwen b and c because there is no delimiter.