Boost logo

Boost Users :

Subject: [Boost-users] [Regex] Search Substring with paranthesis
From: Subhash Nagre (snagre.mumbai_at_[hidden])
Date: 2011-03-15 08:57:23


Hello,
I have a string "(123) Hello" and I want to seach String (123) using
boost::regex,
Also I needed to do a full word search, so I added \b in the end.
Here is my code

std::string searchWord = "(123)";
 searchWord += "\\" + "b"; // Step 1

boost::regex expression;
expression.assign(searchWord);

std::string completeStr = "(123) Hello (123)";
std::string::const_iterator start = completeStr.begin();
std::string::const_iterator end = completeStr.end();

boost::smatch what;
while (boost::regex_search(start, end, what, expression))
{
   int32 foundPos = what.position();
   int32 foundLen = what.length();
   std::string foundString(start + foundPos, start + foundPos + foundLen);
//This foundStr is 123, beacuse it finds a metacharacter in the seach
expression

   // Perform some task with foundString

  start += foundPos + foundLen;
}

How Can I get (123) back and foundPos and foundLen as 0 and 5 respetively. I
tried creating my search expression as /(123/), but it didn't worked.
 Also if I don't add \b in the search expression in Step 1, even in that
case the foundStr is 123 but in that case, the match will be true even for
(1234), which I don't want. I want to find full words only.

Is there is any flag in boost::regex which can help me out or how can I
perform my task.

Thanks in advance

Subhash



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