|
Boost Users : |
From: Eric Niebler (eric_at_[hidden])
Date: 2006-04-12 17:27:40
Lynn Allan wrote:
> Eric Niebler wrote:
>
>>Lynn Allan wrote:
>>
>>>The subsequent searches give valid information about the length and
>>>position, but other fields in the smatch:what variable seem
>>>invalid.
>>>regex_search( altDow.substr(32), what, rex)
>>>regex_search( altDow.substr(45), what, rex)
>>
>>Watch out! altDow.substr(32) is returning a temporary string object.
>>After regex_search returns, the temporary is destroyed and the
>>results
>>object is left holding iterators into a destroyed string.
>
>
> Arggggg. Trying to get "up to speed" with Boost is probably a flawed
> thing to try to do at the same time as learning rudimentary aspects of
> STL. "We're not in MFC-land any more, Toto." <g>
>
> std::string altDow32 = altDow.substr(32);
> if( regex_search( altDow32, what, rex ) ) { unpack }
A better approach would be to use iterators.
// Begin searching 32 characters into altDow
regex_search( altDow.begin() + 32, altDow.end(), what, rex )
Note #1: the match results will now contain iterators into altDow, but
position offsets will be relative to the start of the search, not the
start of altDow.
Note #2: this code is only valid if altDow has at least 32 characters.
Error checking is left as an exercise. :-)
Incidentally, your earlier mistake was distressingly easy to make. I can
think of a way I might be able to catch this mistake at compile time in
a future versions of xpressive.
-- Eric Niebler Boost Consulting www.boost-consulting.com
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