syntagma
syntagma

Reputation: 24334

Tokenizing boost::regex matches

I have created a regex to match the lines of a file which have the following structure: string int int

int main()
{
   std::string line;
   boost::regex pat("\\w\\s\\d\\s\\d");

   while (std::cin)
   {
       std::getline(std::cin, line);
       boost::smatch matches;
       if (boost::regex_match(line, matches, pat))
           std::cout << matches[2] << std::endl;
   }
}

I would like to save those numbers into a pair<string,pair<int,int>>. How can I tokenize match of the boost:regex to achieve this?

Upvotes: 0

Views: 172

Answers (1)

Slava
Slava

Reputation: 44258

First of all your regular expression accepts "one word character then one space then one digit then one space then one digit", I bet this is not what you want. Most probably you want your expression look like:

\w+\s+\d+\s+\d+

where \w+ now means "one or more word characters". If you are sure that there is only one space between tokens you can omit + after \s but this way it is safer.

Then you need to select parts of your expression. That is called sub-expression in RE:

(\w+)\s+(\d+)\s+(\d+)

this way what matches by (\w+) (one or more word characters) will be in matches[1], first (\d+) in matches[2] and second (\d+) in matches[3]. Of course you need to put double \ when you put it in C++ string.

Upvotes: 1

Related Questions