Boost logo

Boost Users :

Subject: Re: [Boost-users] regular expression too complex
From: Eric Niebler (eric_at_[hidden])
Date: 2011-05-24 08:27:33


On 5/24/2011 5:13 PM, Alexander Mingalev wrote:
> On 24.05.2011 12:58, Kraus Philipp wrote:
>>
>> Performance is not my primary aspect. I would like to use a component
>> that can do this, because the remove only runs one time.
>
> Maybe, Boost.Xpressive will work for you.

Yes, it can. Static xpressive has "symbol tables" (a search trie). You
put all the string into a std::map. It'd look something like this:

  #include <string>
  #include <iostream>
  #include <boost/xpressive/xpressive_static.hpp>
  #include <boost/xpressive/regex_actions.hpp>
  using namespace boost::xpressive;

  int main()
  {
      std::map<std::string, char const *> number_map;
      number_map["one"] = "1";
      number_map["two"] = "2";
      number_map["three"] = "3";

      local<std::string> sub;
      sregex rx = icase(a1 = number_map)[sub = a1];

      std::string input = "This ONE has tWo some thrEE strings";
      input = regex_replace(input, rx, sub);

      std::cout << input << '\n';
  }

The above program displays the following:

  This 1 has 2 some 3 strings

It should be pretty quick, too.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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