Regex trouble (compiling a simple expression fails)
Hi, I've looked at replacing the pcre library with boost regex,mainly because the C++ interface seems more usable than the plain C functions from pcre. The trouble is, I'm wasn't able to adjust regular expressions to the format boost regex uses. For instance, while pcre and others seem to like the expression "\?", boost throws bad_expression. The same goes for "\(", "\+" and others I'm using. So, I suppose the question is, is there a flag that can be set for the regex constructor that enables compatibility with a certain regex specification or should the regexp I gave as an example not be accepted ? Any hints would be kindly appreciated. Thanks, Bogdan.
Bogdan Harjoc wrote:
Hi,
I've looked at replacing the pcre library with boost regex,mainly because the C++ interface seems more usable than the plain C functions from pcre.
The trouble is, I'm wasn't able to adjust regular expressions to the format boost regex uses. For instance, while pcre and others seem to like the expression "\?", boost throws bad_expression. The same goes for "\(", "\+" and others I'm using.
You need to escape the escape. "\\?" will match a literal '?' character. Ditto for "\\(" and "\\+". That's because the C++ compiler will eat one level of escape even before the regex engine sees it. Surely you have to use an extra slash with PCRE, too, right? -- Eric Niebler Boost Consulting www.boost-consulting.com
Thanks for the replies !
I noticed I'd forgotten to escape the backslashes only after having
posted. I tried cancelling the post but it obviously didn't work.
Again, thanks and sorry for the mess.
On Apr 5, 2005 5:34 PM, Eric Niebler
Bogdan Harjoc wrote:
Hi,
I've looked at replacing the pcre library with boost regex,mainly because the C++ interface seems more usable than the plain C functions from pcre.
The trouble is, I'm wasn't able to adjust regular expressions to the format boost regex uses. For instance, while pcre and others seem to like the expression "\?", boost throws bad_expression. The same goes for "\(", "\+" and others I'm using.
You need to escape the escape. "\\?" will match a literal '?' character. Ditto for "\\(" and "\\+". That's because the C++ compiler will eat one level of escape even before the regex engine sees it.
Surely you have to use an extra slash with PCRE, too, right?
-- Eric Niebler Boost Consulting www.boost-consulting.com _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I've looked at replacing the pcre library with boost regex,mainly because the C++ interface seems more usable than the plain C functions from pcre.
The trouble is, I'm wasn't able to adjust regular expressions to the format boost regex uses. For instance, while pcre and others seem to like the expression "\?", boost throws bad_expression. The same goes for "\(", "\+" and others I'm using.
So, I suppose the question is, is there a flag that can be set for the regex constructor that enables compatibility with a certain regex specification or should the regexp I gave as an example not be accepted ? Any hints would be kindly appreciated.
It should be completely compatible with pcre - I've even pinched many of pcre's test cases (with permission) - but I suspect you're passing a string literal and forgetting that the compiler will swallow up the first escape, so if you pass "\?" the compiler will change this actually embed the string "?" into the program, and that will cause the engine to throw a bad_expression exception. Use "\\?" if you want to pass a literal backslash to the regex engine. Of course this is a C/C++ issue, not a Boost.Regex one, you'll have the same issue if you pass string literals to pcre. John.
participants (3)
-
Bogdan Harjoc
-
Eric Niebler
-
John Maddock