C++ boost::regex_match 奇怪的行为

C++ boost::regex_match strange behaviour

尝试 boost::regex_match 并得到一个奇怪的行为。

boost::cmatch what;
std::string fn_re_str = R"(\.sig\|\|([a-zA-Z0-9$]+)\()";
boost::regex fn_re(fn_re_str);
if (boost::regex_match("{var d=a[c];if(d.sig||d.s){var e=d.sig||qt(d.", what, fn_re)) {
    std::cout << what[1] << std::endl;
} else {
    std::cerr << "not found" << std::endl;
}

qt 有望找到。

这里https://regex101.com/r/iR9rW5/1找到了。

为什么boost::regex_match找不到?我错过了什么吗?

regex_match 仅匹配完整输入:documentation

⚠ Important

Note that the result is true only if the expression matches the whole of the input sequence. If you want to search for an expression somewhere within the sequence then use regex_search. If you want to match a prefix of the character string then use regex_search with the flag match_continuous set

使用regex_search

Live On Coliru

#include <boost/regex.hpp>
#include <iostream>

int main() {
    boost::cmatch what;
    std::string fn_re_str = R"(\.sig\|\|([a-zA-Z0-9$]+)\()";
    boost::regex fn_re(fn_re_str);
    if (boost::regex_search("{var d=a[c];if(d.sig||d.s){var e=d.sig||qt(d.", what, fn_re)) {
        std::cout << what[1] << std::endl;
    } else {
        std::cerr << "not found" << std::endl;
    }
}

版画

qt