mardi 4 août 2015

C++ stl regex false positive match

I'm using the Visual Studio 2013 IDE with the 2010 compiler on Windows 7.

My regex code is the following:

enum { number, negative, integer, fraction, exponent };

static const std::string reg_expr("(-?)" // Capture the negative.
    "(0|(?:[123456789]\\d*))" // Caputre the integer - either a standalone zero or a non-leading-zero number.
    "(?:\\.(\\d+))?" // Capture the fraction.
    "((?:[eE][-+]?)[123456789]\\d*)?"); // Capture the exponent; sign is optional, must be a non-leadging-zero integer.

std::cmatch matches;
std::regex_search(&*iter, matches, std::regex(reg_expr));

size_t length = 0;

if (matches[integer].matched && (matches[fraction].matched || matches[exponent].matched))
    ret_val = std::stod(&*std::begin(matches[0].str()), &length);
else if (matches[negative].matched && matches[integer].matched && 0 != matches[integer].compare("0"))
    ret_val = std::stoll(&*std::begin(matches[0].str()), &length, 10);
else if (matches[integer].matched)
    ret_val = std::stoull(&*std::begin(matches[0].str()), &length, 10);
else
    throw; // Something useful actually goes here.

"ret_val" is a boost variant.

The problem I'm finding is that the negative sign capture is always positive, whether there is a negative sign or not. It doesn't matter if the number is an integer, or with a decimal, or with an exponent. On a false positive, the capture string is empty. What gives?

Aucun commentaire:

Enregistrer un commentaire