Boost logo

Boost Users :

From: John Maddock (john_maddock_at_[hidden])
Date: 2002-08-24 05:32:49


> It's simple : I have a string and a regular expression. I only want to
find
> the FIRST sub-string which matches my regular expression (S), then to
> extract the sub-string before S and the sub-string after S.
>
> An example :
> my string : "2001 2002 something"
> my regexp : 200\d
>
> Nevermind what regex matches first (either 2001, or 2002). Let's suppose
it
> matches "2002".
> I want to create three strings "2001 ", "2002" and finally " something".

Use regex_search, supplying a -1 index to the match_results class will give
you what perl calls $` and supplying a -2 index gixes you what perl calls $'
(which is what you want in this case).

> That's all, except that I need to do that as QUICK as possible (that's why
> I'm testing another library).
>
> I have another pb. I'm using MFC and CString objects. I didn't see a way
of
> catching the substrings without using a temporary std::string object (it's
> not very efficient because it creates a std::string object, then I need to
> call a cast operator to get a char*, then it creates my CString object
from
> the char*...)
>
> Can you give me some clues ?

I don't know much about CString, but it looks like:

boost::cmatch what;
if(regex_search(...args))
{
CString s(what[0].first, what.length());
}

will do what you want.

John Maddock
http://ourworld.compuserve.com/homepages/john_maddock/index.htm


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