Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2006-08-23 08:09:24


david v wrote:
> Hello,
> I'm trying to adapt the example script regex_search_example.cpp to do
> what i want, that is basically search for all the patterns in a file
> and get their position.
>
> I'm trying to match the pattern "test" in a file that called
> "testfile" that looks like that:
> ############
> file "testfile"
> this_is _the_file_that_contains_twice_the_word_test_test.
> ###########
>
> #######
> In file regex_search_example.cpp i have only changed the line
> const char* re = "test";
> #######
>
> The output looks like that:
>> Processing file testfile
> 1 matches found
> class "" found at position: 56
>
>
>
> Class is empty ?? How can i retrive all the patterns and all the
> positions. I thought the example was supposed to do so ??

Please look more closely at the example code: it creates a std::map of
positions indexed by class name. However, after changing the regex it no
longer has any marked sub-expressions so the code:

m[std::string(what[5].first, what[5].second) + std::string(what[6].first,
what[6].second)] =
what[5].first - file.begin();

Indexes the location of the current match by a null-string - since what[5]
and what[6] are both empty strings - whereas in the original example they
contain the class name and any template parameters respectively.

Finally although both matches are found, the second one is indexed in the
map under the same name as the first, so only one entry gets printed at the
end.

BTW, take a look at regex_iterator: it's probably a better way of
enumerating all the matches in a string.

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