Boost logo

Boost Users :

Subject: [Boost-users] regex
From: Dejan (xamiw_at_[hidden])
Date: 2009-12-13 10:51:23


Hi all,

I have a question regarding boost::regex.

I have one regex which should identify my string AND cut it into pieces.
I've tried with regex_token_iterator and cmatch and failed.

code:

#include <string>
#include <iostream>
#include <boost/regex.hpp>

int
main()
{
  std::string s("s0,s1,s2");
  boost::regex re("^(s[0-9])(,s[0-9])*$");

  // 1. attempt
  boost::sregex_token_iterator i(s.begin(), s.end(), re), j;
  while(i!=j)
  {
    std::cout << *i++ << std::endl;
  }

  // 2. attempt
  boost::cmatch matches;
  if(boost::regex_search(s.c_str(), matches, re))
  {
    for(int p=0, q=matches.size(); p<q; p++)
    {
      std::string match(matches[p].first, matches[p].second);
      std::cout << match << std::endl;
    }
  }
}

output:

s0,s1,s2
s0,s1,s2
s0
,s2

desired output:

s0,s1,s2
s0
,s1
,s2

Any hints how to do this in one step?

Thanks!


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net