简体   繁体   English

C ++子字符串包含在2个特定字符之间

[英]C++ substring contained between 2 specific characters

In c++ would like to extract all substrings in a string contained between specific characters, as example: 在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;

result of this call should be: 该调用的结果应为:

MON 3
1

also using boost if needed. 如果需要,还可以使用升压。

You could try Regex: 您可以尝试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 . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM