H'H
H'H

Reputation: 1678

std::regex_error at memory location

I have a regex as:

std::regex regexp(
        R"(\$\ part,\ model.*[\n\r]([-]?[0-9]*\.?[0-9]+[Ee]?[-+]?[0-9]*),([-]?[0-9]*\.?[0-9]+[Ee]?[-+]?[0-9]*))",std::regex::extended);

The code compiles, but I receive the following error:

Unhandled exception at 0x748F49C2 in regex.exe: Microsoft C++ exception: std::regex_error at memory location 0x00EFEE30.

Upvotes: 1

Views: 721

Answers (1)

rustyx
rustyx

Reputation: 85341

According to the regex spec, the effect of escaping a space (which is not a special character) is undefined:

9.4.2 ERE Ordinary Characters

... The interpretation of an ordinary character preceded by an unescaped <backslash> ( \ ) is undefined, except in the context of a bracket expression ...

Apparently in the MSVC implementation, std::regex_error gets thrown.

After fixing the escaping the regex compiles.

try {
    std::regex regexp(
        R"(\$ part, model.*[\n\r]([-]?[0-9]*\.?[0-9]+[Ee]?[-+]?[0-9]*),([-]?[0-9]*\.?[0-9]+[Ee]?[-+]?[0-9]*))", std::regex::extended);
}
catch (std::exception const& e) {
    std::cerr << e.what() << std::endl;
}

Upvotes: 2

Related Questions