std :: regexp的二次行为?

时间:2018-06-10 15:26:23

标签: c++ c++11

我正在使用解析器组合器库,并且我有一个regexp组合器,用于检查流是否与当前位置的正则表达式匹配。这就是它的样子:

// execute parser
maybe<parse_match> operator()(parse_stream& stream) const {
    using std::regex;
    using std::cmatch;
    using std::regex_search;

    cmatch matches;
    if (regex_search(&stream, matches, regex_, regex_constants::match_continuous)) {
        return stream.match(matches.str(0).size());
    }
    return maybe_error("no match for '" + exp_ + "'");
}

匹配并返回包含指向匹配子字符串的指针的结构或返回错误。 &amp; stream解析为const char *并且我使用cmatch,所以它不应该从底层char流构建一个字符串。

调试一些性能问题的问题是,这似乎每次消化整个字符串。如果我解析一个浮点数列表(例如:[3.14,4.1348,3.10134,...]并打印每个正则表达式匹配所花费的时间,那么显然是对字符串I&#39的线性依赖性已经解析过,开头显示指数行为!

Regexp match time

这是regex_search的预期行为吗?有没有办法从较大的字符串中检索一个匹配?似乎没有看到文档...

编辑第一个:我只使用optimize标志构建正则表达式:

regexp(string exp)
    : exp_(exp), regex_(exp, regex_constants::optimize) {}

编辑第二个:PCRE2用pcre2_match做我想要的,所以也许他们只是把这个部分搞错了......

0 个答案:

没有答案