Kenneth
Kenneth

Reputation: 555

Boost Regex will not Compile

I am in the process of converting a PHP program to C++ for speed and memory management reasons. Because PHP uses Perl-Compatible syntax, I went the boost library which is also uses the Perl syntax

Using Visual Studio Express 2013, I was able to use 6 of my 7 expressions as is. The one giving me fits is below

^([\w]+= {\s*(?:[a-f0-9]{2}\s+)*})\s*

Used in code as follows:

#include <string>
#include <boost/regex.hpp>
std::string regexError = R"~(^([\w]+= {\s*(?:[a-f0-9]{2}\s+)*})\s*)~";
boost::regex e(regexError);

This expression matches a string like this Regex101 Example:

MASKSUBSYS= { 00 af 01 02 }

I'm getting the following run time exception when calling boost::regex e(regexError);

Unhandled exception at 0x7515C41F in RegexTest.exe: Microsoft C++ exception: boost::exception_detail::clone_impl > at memory location 0x002DED5C.

It looks like there is no issue with blackslash escaping as you can see from the raw string read into memory.

String in memory

Like I said, only 1 of the 7 expressions will not compile straight out of PHP. I'm guessing it has something to do with regex fundamentals, but I am not versed enough in regex enough to recognize it. This isn't even the most complicated expression by far! Any ideas?

Upvotes: 1

Views: 169

Answers (1)

hwnd
hwnd

Reputation: 70742

The problem most likely is that the curly bracket { } metacharacters need to be escaped as well. And instead of placing a character class around \w, you can write it out by itself as follows.

R"~(^(\w+= \{\s*(?:[a-f0-9]{2}\s+)*\})\s*)~"

Code Demo

Upvotes: 1

Related Questions