Boost logo

Boost :

From: George A. Heintzelman (georgeh_at_[hidden])
Date: 2001-08-29 11:03:40


Hi,

I'm using the regex library quite successfully in my work. I have run
into a problem where I am matching a regex including something like
this:

boost::regex rx("TEST=(.*)\\?(.*)");

Then, I want to test some other value, call it v, against the first
submatch:

// It would be nice to have a boost:: typedef for this, but I digress...
boost::match_results<std::string::const_iterator> results;

if (boost::regex_match(str,results,rx)) {

// This is what I would like to write:
// if (results[1] == v)
// This also doesn't work:
// if (results[1] == v)
// This is the needed formulation:
   if (string(results[1]) == v)
// This would work too...
   if (std::string(results[1].first,results[1].second) == v) {
        // ...
   }

In both formulations using operator== directly, the implicit conversion
to basic_string<char> doesn't fire. In the first case, it is because
sub_match has its own operator== member which gets found first, and
does not match. In the second case, I think it doesn't fire because the
standard library operator== is a template and so doesn't consider
conversions.

Anyway, it would be nice if this were possible without an explicit cast.

BTW, I noticed in the implementation of sub_match::operator
basic_string<char_type> that the regex library computes the length and
does the reserve on its own. Wouldn't it be better to use the string
constructor directly, letting the string library do that optimization
(and possibly others it might know about), as it ought to?

        return basic_string<char_type>(first,second);

is a lot more concise... It might fail for mismatched iterators in some
cases, but I think those compilers it is going to fail on anyway. Right?

George Heintzelman
georgeh_at_[hidden]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk