"John Maddock"
wrote in message news:00b701c7648e$1b5e3180$69d36b51@fuji... Jonathon Douglas wrote:
I am trying to convert the following C# code over to using boost::regex
MatchCollection col = new Regex(@"\w+").Matches(@"slx://account/123456"); string table = col[1].Value; string id = col[2].Value;
Table would equal "account" and id would equal "123456" in this.
Ive tried using regex_match with this but it always finds no matches.
Any sugestions ?
C++ gobbles up \'s in starings, so it looks you want "\\w+" so that
Jonathon Douglas wrote: the
regex engine gets to see \w+
John.
Hi,
Ive tried that, however been a complete newbie to boost im probably still missing something obvious. Heres the test code so far
#include
#include <iostream> #include <string> #include using namespace std; int main (int argc, char *argv[]) { string uri ("slx://account/12345"); boost::regex re ("\\w+"); boost::cmatch what; if (boost::regex_match(uri.c_str(), what, re)) { cout << what[0] << endl; cout << what[1] << endl; cout << what[2] << endl; } else { cout << "nowt matched" << endl; } return 0; }
It looks like there is some confusion about boost::regex_match does. It attempts to match the entire string against the regular expression (imagine an implicit ^ and $ around the expression). I am not familiar with the C# library, but it looks like "Matches" finds all of the matches in the string and returns an array of them. Is this correct? If it is, then there are two things you can do: Option 1: Change your expression to use captures. It might look like "slx://(\\w+)/(\\w+)". If you drop this ion your code above, you should see that what[0] is the entire uri, what[1] is "account" and what[2] is "12345". I generally prefer this method, as it also provides syntax checking. Option 2: Use boost::regex_iterator. Perhaps something like: //WARNING: completely untested code. I am just threw this together from information I found in http://www.boost.org/libs/regex/doc/regex_iterator.html string uri ("slx://account/12345"); boost::regex re ("\\w+"); boost::sregex_iterator ri (uri.begin(), uri.end(), re); boost::sregex_iterator end; //An uninitialized regex_iterator works as end while (ri != end) { //Dereference the iterator to get a boost::match_results object, then take match_results[0] to get the whole match. std::cout << (*ri)[0] << std::endl; ++ri; //Advance to the next match }