在c ++中,我想提取特定字符之间包含的字符串中的所有子字符串,例如:
std::string str = "XPOINT:D#{MON 3};S#{1}"
std::vector<std:string> subsplit = my_needed_magic_function(str,"{}");
std::vector<int>::iterator it = subsplit.begin();
for(;it!=subsplit.end(),it++) std::cout<<*it<<endl;
此调用的结果应为:
MON 3
1
如果需要也使用提升。
答案 0 :(得分:1)
You could try Regex:
#include <iostream>
#include <iterator>
#include <string>
#include <regex>
int main()
{
std::string s = "XPOINT:D#{MON 3};S#{1}.";
std::regex word_regex(R"(\{(.*?)\})");
auto first = std::sregex_iterator(s.begin(), s.end(), word_regex),
last = std::sregex_iterator();;
while (first != last)
std::cout << first++->str() << ' ';
}
Prints
{MON 3} {1}
Demo.