I seem to be having some trouble with the regex library:
#include
#include <cassert>
#include <iostream>
int main(int argc, char* argv[])
{
using namespace boost;
cmatch what;
assert(regex_match("foo: bar", what, regex("([\\w]+):[\\s]*(.*)")));
assert(what.size() == 3);
assert(what[1].matched);
assert(what[2].matched);
assert(std::string("foo") == std::string(what[1]));
std::cout << std::string(what[2]) << "\n";
assert(std::string("bar") == std::string(what[2])); // fails
return 0;
}
The last assert always fails, because regex::match insists on including the
space after the ":" in the second match result, which seems wrong to me.
I've tried giving various different combinations of flags to the regex ctor,
although from what I can tell from the documentation the default flags
should work. This is with VC7 and boost 1.30.2. For comparison, perl
(5.6.1) does not seem to have this problem, as the following script
illustrates:
#!/usr/bin/perl
print "matches: <$1> <$2>\n" if ('foo: bar' =~ /([\w]+):[\s]*(.*)/);
Is there any way to get the behavior I want?
Thanks,
Scott McCaskill