C ++通过定界符分割字符串并保持定界符在结果中 [英] C++ spliting string by delimiters and keeping the delimiters in result
问题描述
我正在寻找一种使用C ++中的regex通过多个定界符来分割字符串的方法,但又不会丢失输出中的定界符,而是将定界符与分割后的部分保持顺序,例如:
I'm looking for a way to split string by multiple delimiters using regex in C++ but without losing the delimiters in output, keeping the delimiters with splitted parts in order, for example:
输入
aaa,bbb.ccc,ddd-eee;
aaa,bbb.ccc,ddd-eee;
输出
aaa,bbb。 ccc,ddd-eee;
aaa , bbb . ccc , ddd - eee ;
我已经找到了一些解决方案,但都是在C#或Java中,寻找一些C ++解决方案,最好不要使用Boost。
I've found some solutions for this but all in C# or java, looking for some C++ solution, preferably without using Boost.
推荐答案
您可以在 regex_iterator
。例如,如果您知道分隔符是逗号,句点,分号和连字符,则可以使用捕获分隔符或一系列非分隔符的正则表达式:
You could build your solution on top of the example for regex_iterator
. If, for example, you know your delimiters are comma, period, semicolon, and hyphen, you could use a regex that captures either a delimiter or a series of non-delimiters:
([.,;-]|[^.,;-]+)
将其拖放到示例代码中,最后得到类似的内容:
Drop that into the sample code and you end up with something like this:
#include <iostream>
#include <string>
#include <regex>
int main ()
{
// the following two lines are edited; the remainder are directly from the reference.
std::string s ("aaa,bbb.ccc,ddd-eee;");
std::regex e ("([.,;-]|[^.,;-]+)"); // matches delimiters or consecutive non-delimiters
std::regex_iterator<std::string::iterator> rit ( s.begin(), s.end(), e );
std::regex_iterator<std::string::iterator> rend;
while (rit!=rend) {
std::cout << rit->str() << std::endl;
++rit;
}
return 0;
}
尝试替换您喜欢的任何其他正则表达式。
Try substituting in any other regular expressions you like.
这篇关于C ++通过定界符分割字符串并保持定界符在结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!