Boost logo

Boost :

Subject: Re: [boost] [xpressive] Is there a way to test for an empty regex?
From: Michael Goldshteyn (mgoldshteyn_at_[hidden])
Date: 2008-11-19 20:23:24


"Eric Niebler" <eric_at_[hidden]> wrote in message
news:4924898A.4020101_at_boost-consulting.com...
> Michael Goldshteyn wrote:
>> What is the best way to test for an empty boost::xpressive::regex object?
>>
>> That is, if I have code like:
>>
>> boost::xpressive::sregex re;
>>
>> ...
>>
>> // Test if re is empty and should not be used to perform a regex_search
>> if (?????)
>
> There is, although it's not obvious from the documentation that it's
> possible. You can see from the postcondition on the default constructor of
> basic_regex that this->regex_id() == 0 for default-constructed regex
> objects and != 0 for non-default constructed regex objects. So your test
> above can be:
>
> if( 0 != re.regex_id() )
>
> HTH,

Thanks for the speedy reply. I did notice the postcondition on the
constructor and the fact that the regex_id() will return 0 for a
non-initialized regex, but was hoping that there was a more intuitive way to
go about this test, since readers of the code may get confused without a
comment. How hard would it be to add an empty() function with a signature
that is similar to the one in boost::regex before Boost 1.38.0 comes out,
since this functionality, at least in my humble opinion is very useful.

The signature for the function in boost::regex is:

bool empty() const;

Thanks,

Michael Goldshteyn


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