[Regex] Word boundary pattern does not match

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

Teddy Brielle wrote:
Hi,
I use the boost v1.35 regex library on Windows XP.
Have I done something wrong in my pattern or in my code (match flags,...) ?
I think this is a bug: currently \B only matches *within a word*, but I've checked what Perl does and that matches anything that isn't a word boundary, including those outside of a word. There's an easy fix that I'm committing, but in the mean time you could use: (?<=\W)\*(?=\W) as an alternative. HTH, John.
participants (2)
-
John Maddock
-
Teddy Brielle