regex expression core dumps with \\
Hi there, I've been using regex++ (v 3) for a while now. It's great! I just found out that my regex core dumps my program, look: std::string StringUtilities::getMsqlEscapedString( const char* input ) { if ( input == NULL ) return ""; cerr << "Hello" << endl; std::string inputStr( input ); cerr << "Hello111 (" << inputStr << ")" << endl; static const boost::regex expression1( "\\" ); cerr << "Hello222" << endl; string temp = boost::regex_merge( inputStr, expression1, "\\\\" ); cerr << "Hello33" << endl; static const boost::regex expression2( "'" ); return boost::regex_merge( temp, expression2, "\\'" ); } // StringUtilities::getMsqlEscapedString That's all I have. When I call it I see this: Testing getMsqlEscapedString... Hello Hello111 (hello) Aborted (core dumped) So, it seems that the call to: static const boost::regex expression1( "\\" ); makes it core dump. I went and changed that regular expression by "\"", just for kicks, and it works; it doesn't do what I want but it doesn't core dump. Anything I should do to avoid this problem? thanks, -Anibal
On 3/15/02 5:11 PM, "soleani" <anibal@videotron.ca> wrote:
So, it seems that the call to:
static const boost::regex expression1( "\\" );
makes it core dump.
Not sure why you got a core dump, but I do know what the problem is. The regular expression code is throwing an exception because your expression is bad. Try this program: #include <iostream> #include <boost/regex.hpp> int main() { try { boost::regex expression("\\"); } catch (const boost::bad_pattern& p) { std::cout << "bad pattern: " << p.what() << '\n'; } } When you run it, you'll see that the expression causes a "bad_pattern" exception because it has a backslash at the end. The problem is that you have to escape the backslash once because of how C literals are interpreted and again because of how characters in regular expressions are interpreted. One fix is to use "\\\\" instead of "\\" so the backslash is escaped for the regular expression: boost::regex expression("\\\\"); Another is to use the "literal" flag when creating the regular expression so the backslash doesn't need to be escaped: boost::regex expression("\\", boost::regbase::literal); Hope that helps. -- Darin
participants (2)
-
Darin Adler
-
soleani