Boost logo

Boost :

From: Teddy Brielle (tbrielle_at_[hidden])
Date: 2008-05-12 10:31:46


Hi,

I use the boost v1.35 regex library on Windows XP.

I would like to find the '*' character from an input string only if this
character is not bound to a word (for replacement purpose). I then use
the following regular expression pattern to find occurrences in my input
string:

\B\*\B

But this regular expression seems not to match any '*' character of the
following input string:

* test *test test* * te*st *

I would expect that the regular expression would match the 1st, 4th and
6th '*' character.
I have performed further tests with the pattern \b*\b, which works and
matches the 5th '*' character.

Here is the code I use :

void Replace(std::string& string, std::string& pattern, std::string&
replacementString)
{
    try
    {
        std::string newString;

        // Looking for matching substrings (case insensitive)
        boost::regex expression (CStringToStdString(pattern),
boost::regex::perl|boost::regex::icase);

        std::string::const_iterator start = string.begin();
        std::string::const_iterator end = string.end();

        boost::match_results<std::string::const_iterator> what;
        boost::match_flag_type flags = boost::match_default;
        while (boost::regex_search(start, end, what, expression, flags))
        {
            // Replace the matching occurrence with the replacement string
            newString = newString + std::string(start, what[0].first);
            newString = newString + replacementString;

            // Next...
            start = what[0].second;
        }
        newString = newString + std::string(start, string.end());

        string = newString;
    }
    catch (std::runtime_error e)
    {
        // Return string as is.
        TRACE(e.what());
    }
}

and here are the provided parameters:

string = "* test *test test* * te*st *";
pattern = "\\B\\*\\B";
replacementString = "";

Have I done something wrong in my pattern or in my code (match flags,...) ?

Thanks in advance for your help.

Regards,

Teddy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk