|
Boost Users : |
From: John Maddock (john_at_[hidden])
Date: 2006-06-08 07:50:15
dave_at_[hidden] wrote:
> Hi, im having trouble with a base case for regex_search and the
> number of hits it finds in a haystack. I am wondering if i am not
> setting flags that need to be set, but i cannot find anything for
> this in the docs. My usage only seems to find the string "HELLO"
> once, as the cout indicates xcmMatch.size() is 1 after i run this. I
> am using boost libs 1.31.00 . Thanks!
You're misunderstanding what match_results contains and what regex_search
does:
regex_search finds the *first* possible match *only*, the match_results
structure contains that match plus any marked sub-expressions, so had your
expression been "(HEL)(LOW)" then match_result::size() would have returned
3: item 0 would have been "HELLO", item 1 "HEL", and item 2 "LOW".
If you want to find all the matches in a string then you have to iterate
through them: try regex_iterator or if you just want the string and don't
care about sub-expression matches then regex_token_iterator will also do
what you want.
Finally I note in your code that you're calling c_str() on the strings and
then passing that to the algorithms: that's inefficient, the first thing
that the algorithm has to do then is determine how long the string is, but
hold on, std::string already knows that! Pass the string to the algorithm
instead, it won't make much difference for short strings, but if you have a
lot of longish strings to match it makes a huge difference.
John.
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