[英]C++ split string on multiple substrings
In C++, I'd like to something similar to: 在C ++中,我想要类似于:
However, I'd like to specify more than one substring to split on. 但是,我想指定多个要分割的子字符串。 For example, I'd like to split on "+", "foo", "ba" for the string "fda+hifoolkjba4" into a vector of "fda", "hi", "lkj", "4".
例如,我想将字符串“fda + hifoolkjba4”的“+”,“foo”,“ba”拆分为“fda”,“hi”,“lkj”,“4”的向量。 Any suggestions?
有什么建议么? Preferably within STL and Boost (I'd rather not have to incorporate the Qt framework or additional libraries if I can avoid it).
最好是在STL和Boost中(如果我能避免的话,我宁愿不必加入Qt框架或其他库)。
I would go with regular expressions, either from <regex>
or <boost/regex.hpp>
; 我会使用正则表达式,来自
<regex>
或<boost/regex.hpp>
; the regular expression you need would be something like (.*?)(\\+|foo|ba)
(plus the final token). 你需要的正则表达式就像
(.*?)(\\+|foo|ba)
(加上最终的标记)。
Here's a cut-down example using Boost: 这是使用Boost的简化示例:
std::string str(argv[1]);
boost::regex r("(.*?)(\\+|foo|ba)");
boost::sregex_iterator rt(str.begin(), str.end(), r), rend;
std::string final;
for ( ; rt != rend; ++rt)
{
std::cout << (*rt)[1] << std::endl;
final = rt->suffix();
}
std::cout << final << std::endl;
I suggest using regular expression support in boost. 我建议在boost中使用正则表达式支持。 See here for an example.
请看这里的例子。
here is a sample code that can split the string: 这是一个可以拆分字符串的示例代码:
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
int main()
{
boost::regex re("(\\+|foo|ba)");
std::string s("fda+hifoolkjba4");
boost::sregex_token_iterator i(s.begin(), s.end(), re, -1);
boost::sregex_token_iterator j;
while (i != j) {
std::cout << *i++ << " ";
}
std::cout << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.