Newbie conversion of C# to boost::regex

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 ? Thanks, JD

"John Maddock"
Hi,
Ive tried that, however been a complete newbie to boost im probably still
missing something obvious. Heres the test code so far
#include

Jonathon Douglas wrote: the
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 }

Jonathon Douglas wrote:
Ive tried that, however been a complete newbie to boost im probably still missing something obvious. Heres the test code so far
If you want to *find* some text within a string then you need to call regex_search. regex_match only succeeds if *all* of the text can be matched by the expression (think validating data input). John.
participants (3)
-
Andrew Holden
-
John Maddock
-
Jonathon Douglas