3

I need to separate a string like this:

cat, dog , ant( elephant, lion(tiger)), bird

into this:

cat
dog
ant( elephant, lion(tiger))
bird

My current state is this: (\w+)(,\s*)*, but that also separates elephant, lion and tiger. Further, some commas and spaces are kept.

You might have guessed, that I will call the same expression again on the ant(...) string in a further iteration. If important, I'll use this in c++.

miken32
  • 42,008
  • 16
  • 111
  • 154
dani
  • 3,677
  • 4
  • 26
  • 60

1 Answers1

3

This regex:

(\w+\(.+\))|\w+

Will parse

cat, dog , ant( elephant, lion(tiger)), bird

Into:

cat
dog
ant( elephant, lion(tiger))
bird

Full program:

#include <string>
#include <vector>
#include <iterator>
#include <regex>
#include <iostream>

int main()
{
    std::string str{R"(cat, dog , ant( elephant, lion(tiger)), bird)"};
    std::regex r{R"((\w+\(.+\))|\w+)"};

    std::vector<std::string> result{};
    auto it = std::sregex_iterator(str.begin(), str.end(), r);
    auto end = std::sregex_iterator();
    for(; it != end; ++it) {
        auto match = *it;
        result.push_back(match[0].str());
    }
    std::cout << "Input string: " << str << '\n';
    std::cout << "Result:\n";
    for(auto i : result)
        std::cout << i << '\n';
}

live demo

wally
  • 10,717
  • 5
  • 39
  • 72
  • Thank you for your answer, will test it tomorrow. What function does the `R` in front of the `string`/`regex` have? – dani Jan 15 '17 at 20:53
  • @dani It is a [raw string literal](http://en.cppreference.com/w/cpp/language/string_literal) and it [makes it possible](http://stackoverflow.com/q/19075999/1460794) to write the regex string [without escaping](http://en.cppreference.com/w/cpp/language/escape) all the slashes. – wally Jan 15 '17 at 21:10