Separating strings

I have a string like: phrase ({hello} & {good})
The string is called comando and the words inside the brackets can be anything.

How can I get the words inside separatly ? It can be in diferent strings.

Like:

string one = hello;
string two = good;

I really need this :'(
Regular expressions: http://en.cppreference.com/w/cpp/regex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <regex>
#include <iostream>
#include <iomanip>

int main()
{
    for( const std::string comando : { "{hello} & {good}", "{cats} and {dogs}", "{hello}{dogs}" } )
    {
        // \{(\w+)\} : one or more alpanumeric characters (marked sub-expression) within a pair of braces 
        // .* : zero or more characters of any kind 
        static const std::regex words_re( R"(\{(\w+)\}.*\{(\w+)\})" ) ;
        
        std::smatch match_results ;
        if( std::regex_match( comando, match_results, words_re ) )
        {
            const std::string first = match_results[1] ; // marked sub-expression #1
            const std::string second = match_results[2] ; //  marked sub-expression #2
            std::cout << "comando: " << std::quoted(comando) << "  =>  first: " << std::quoted(first) 
                      << "  second: " << std::quoted(second) << '\n' ;
        }
        else std::cerr << "the pattern was not matched\n" ;
    }
}

http://coliru.stacked-crooked.com/a/71f8b2839d9ee595
Topic archived. No new replies allowed.