[Regex] Avoid exceptions when using invalid regular expressions

Hi, I'm trying to use boost-regex in my project that doesn't support exceptions. Thus, when constructing a regular expression with boost::regex expression(expr); and passig an invalid regular expression, my program crashes. What can I do to make sure my regular expression is valid before passing it to exception-throwing methods like that one? I already found an older answer that seems to apply to my problem (http://lists.boost.org/Archives/boost/2002/11/39161.php), it is suggested to use the member function set_expression. But how do I do that? I only get a lot of compiler errors. I have the regular expression as a std::string, so I'd like to write something like //string expr holds the regular expression boost::regex expression(); int result = expression.set_expression(expr); //check whether result is 0 -> ok. Thanks in advance for your help! Best regards, Anne Martens

"Anne Martens" <mail@anne-martens.de> wrote in message news:443140C5.6020007@anne-martens.de...
Hi,
I'm trying to use boost-regex in my project that doesn't support exceptions. Thus, when constructing a regular expression with boost::regex expression(expr); and passig an invalid regular expression, my program crashes.
What can I do to make sure my regular expression is valid before passing it to exception-throwing methods like that one?
One possibility is to use a try-catch inside your function that produces the regex, and return optional<regex> optional<regex> maybe_make_regex(const string &s) { try { return regex(s); } catch (...) { return none; } } Joe Gottman

I'm trying to use boost-regex in my project that doesn't support exceptions. Thus, when constructing a regular expression with boost::regex expression(expr); and passig an invalid regular expression, my program crashes.
What can I do to make sure my regular expression is valid before passing it to exception-throwing methods like that one?
There is a no_except flag that can be passed the regex constructor/assign methods to disable throwing of exceptions: boost::regex e("bad**string", boost::regex_constants::perl | boost::regex_constants::no_except); if(e.empty()) { std::cout << "oops" << std::endl; } However, I notice that I removed all mention of this from the docs when I updated the interface to match the TR1 spec. It's non-the-less still supported however. John.

John Maddock wrote:
There is a no_except flag that can be passed the regex constructor/assign methods to disable throwing of exceptions:
boost::regex e("bad**string", boost::regex_constants::perl | boost::regex_constants::no_except);
if(e.empty()) { std::cout << "oops" << std::endl; }
However, I notice that I removed all mention of this from the docs when I updated the interface to match the TR1 spec. It's non-the-less still supported however.
Hi John, Was that an oversight, or an explicit decision made as part of the "standardisation"? I'd hate for the latter to be true (much as I prefer exceptions when available for this sort of handling). Regards, [)o IhIL..

Was that an oversight, or an explicit decision made as part of the "standardisation"? I'd hate for the latter to be true (much as I prefer exceptions when available for this sort of handling).
I cut out a lot of stuff that wasn't in the std proposal if there was a better alternative. In this case I may have been over-zealous, or at least not paying enough attention. It is worth preserving IMO, I'll update the docs when I have a minute. John.

Joe Gottman wrote:
One possibility is to use a try-catch inside your function that produces the regex, and return optional<regex>
optional<regex> maybe_make_regex(const string &s) { try { return regex(s); } catch (...) { return none; } }
Joe Gottman
That's not a possibility. The OP said that his environment does not support exception, i.e. every boost::throw_exception call leads to immediate program abortion. There is no exception to catch, and try, catch and throw probably don't even compile. There seems to be no documented solution to the problem. However, basic_regex has indeed an undocumented but public set_expression() member, which throws no exception but instead returns status(), another undocumented but public member. Or you can simulate this call by passing the undocumented regex_constants::no_except flag to the constructor and testing status() for yourself. Note that none of the undocumented elements here appear in the standard proposal either. Sebastian Redl
participants (5)
-
Anne Martens
-
Joe Gottman
-
John Maddock
-
Phil Nash
-
Sebastian Redl