I’m struggling on this one and I’m to a point where I not making any headway and it’s time to ask for help. My familiarity with the boost libraries is only slightly better than superficial. I’m trying to do a progressive scan through a rather large string. In fact, it’s the entire contents of a file read into a std::string object (the file isn’t going to be that large, it’s the output from a command line program).
The output of this program, pnputil, is repetitive. I’m looking for certain patterns in an effort to find the “oemNNN.inf” file I want. Essentially, my algorithm is to find the first “oemNNN.inf”, search for identifying characteristics for that file. If it’s not the one I want, move on to the next.
In code, it’s something like:
std::string filesContents;
std::string::size_type index(filesContents.find_first_of("oem"));
std::string::iterator start(filesContents.begin() + index);
boost::match_results<std::string::const_iterator> matches;
while(!found) {
if(boost::regex_search(start, filesContents.end(), matches, re))
{
// do important stuff with the matches
found = true; // found is used outside of loop too
break;
}
index = filesContents.find_first_of("oem", index + 1);
if(std::string::npos == index) break;
start = filesContents.being() + index;
}
I’m using this example from the boost library documentation for 1.47 (the version I’m using). Someone please explain to me how my usage differs from what this example has (aside from the fact that I’m not storing stuff into maps and such).
From what I can tell, I’m using the same type of iterators the example uses. Yet, when I compile the code, Microsoft’s compiler tells me that: no instance of overloaded function boost::regex_search matches argument list. Yet, the intellisense shows this function with the arguments I’m using, although the iterators are named something BidiIterator. I don’t know the significance of this, but given the example, I’m assuming that whatever the BidiIterator is, it takes a std::string::iterator for construction (perhaps a bad assumption, but seems to make sense given the example). The example does show a fifth argument, match_flags, but that argument is defaulted to the value: boost::match_default. Therefore, it should be unnecessary. However, just for kicks and grins, I’ve added that fifth argument and still it doesn’t work. How am I misusing the arguments? Especially, when considering the example.
Below is a simple program which demonstrates the problem without the looping algorithm.
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main() {
std::string haystack("This is a string which contains stuff I want to find");
boost::regex needle("stuff");
boost::match_results<std::string::const_iterator> what;
if(boost::regex_search(haystack.begin(), haystack.end(), what, needle, boost::match_default)) {
std::cout << "Found some matches" << std::endl;
std::cout << what[0].first << std::endl;
}
return 0;
}
If you decide to compile, I am compiling and linking against 1.47 of the boost library. The project that I’m working with uses this version extensively and updating isn’t for me to decide.
Thanks for any help. This is most frustrating.
Andy
In general iterator’s types are different.
returning values from
begin()andend()will bestd::string::iterator.But your match type is
std::string::iteratorandstd::string::const_iteratorare different types.So there is few variants
const std::string haystack;)std::string::const_iterator begin = haystack.begin(), end = haystack.end();) and pass them toregex_search.boost::match_results<std::string::iterator> what;haystack.cbegin()andhaystack.cend()example of work