Boost logo

Boost Users :

Subject: Re: [Boost-users] Can Regex callback function arguments be modified?
From: John Maddock (john_at_[hidden])
Date: 2009-09-20 06:59:51


> My data is structured like this:
>
> [Unique ID1]
> Variable Name = Variable Value
> Variable Name = Variable Value
> Variable Name = Variable Value
>
> [Unique ID2]
> Variable Name = Variable Value
> Variable Name = Variable Value
> Variable Name = Variable Value
> Variable Name = Variable Value
>
> All I need to do is to extract the different parts, to be saved in a
> map. In my first callback, I divide every section into (a) Unique ID,
> and (b) rest of the (multiline) block. Once I have the latter, I make a
> call to a second callback to break the LHS and the RHS of each
> assignment expression.
>
> So you say that the callback is really not required?

How about nested for-loops (caution untested code!!):

std::string text = get_text_from_file();
regex block_extractor("\\[(\\w+)\\]([^\\[]+)");
regex variable_extractor("(\\w+)\\s*=\\s*(\\w+)");

for(regex_iterator<std::string::const_iterator> i =
make_regex_iterator(text, block_extractor);
        i != regex_iterator<std::string::const_iterator>(); ++i)
{
   std::string block_name = i->str(1);
   std::string block_contents = i->str(2);
   std::map<std::string, std::string> param_list;

  for(regex_iterator<std::string::const_iterator> j =
make_regex_iterator(block_contents, variable_extractor);
        j != regex_iterator<std::string::const_iterator>(); ++j)
  {
      param_list[j->str(1)] = j->str(2);
  }
}

Obviously the regexes used may vary depending upon the exact data format,
but hopefully this should give you the general idea?

John.


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