2

Trying boost::regex_match and got a strange behaviour.

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 is expected to be found.

Here https://regex101.com/r/iR9rW5/1 it is found.

Why boost::regex_match can't find it? Do I miss something?

Zelid
  • 6,905
  • 11
  • 52
  • 76

1 Answers1

3

regex_match only matches full input: 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

Use 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;
    }
}

Prints

qt
sehe
  • 374,641
  • 47
  • 450
  • 633