Boost logo

Boost Users :

From: Andrew McDonald (andrew.mcdonald_at_[hidden])
Date: 2006-05-22 23:02:35


The iostreams library has a basic_regex_filter that will do this job.

or in context
4. Reference -> 4.2 Classes -> basic_regex_filter

The following ctor is the one you probably want
basic_regex_filter( const basic_regex<Ch, Tr, Alloc>& pattern,
                        const formatter& replace,
                        regex_constants::match_flag_type flags =
                            regex_constants::match_default );

It takes a regex (and regex flags) and for each match calls the formatter function.
The formatter is a boost::function1 that takes the match_results and is expected to return a string that will be used to replace the match.

You will probably have to put your src/dst strings into stringstreams to use the filter.

Note that the library does support a generous additional variety of sinks and sources , files, arrays, zip archives etc.

Please note that I recently found a bug in basic_regex_filter in cases where no match is found. See my earlier posts for a fix.



> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]]On Behalf Of Johan Nilsson
> Sent: Monday, 22 May 2006 7:53 PM
> To: boost-users_at_[hidden]
> Subject: [Boost-users] [Regex] Replace algorithm
> Hi,
> I'd like to replace all parts of a string with the results of
> calling a
> function, passing the match as a parameter. Some pseudo-code:
> ---
> std::string replacer(smatch& s)
> {
> return std::getenv(s[1].c_str());
> }
> std::string const input = "This is ${USER} on ${HOST}";
> boost::regex const re("\\$\\{([^\\}]+)\\}");
> std::string result = boost::regex_replace(input, re,
> boost::bind(&replacer));
> assert("This is Me on MyWorkstation" == result);
> ---
> (I'm sure there are errors above, but I hope you get the
> general idea).
> Any easy way to do something similar?
> // Johan
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at