1

i wannna get keyword matching length

but, always match count is zero

why..?

string text = "sp_call('%1','%2','%a');";
std::regex regexp("%[0-9]");
std::smatch m;
std::regex_match(text, m, regexp);
int length = m.size();
cout << text.c_str() << " matched " << length << endl; // always 0
Biz Chepp
  • 21
  • 3

1 Answers1

0

regex_match

Determines if the regular expression e matches the entire target character sequence, which may be specified as std::string, a C-string, or an iterator pair.

You need to use regex_search

Determines if there is a match between the regular expression e and some subsequence in the target character sequence.

Also you can use regex_iterator, example from here:

string text = "sp_call('%1','%2','%a');";
std::regex regexp("%[0-9]");

auto words_begin =
    std::sregex_iterator(text.begin(), text.end(), regexp);
auto words_end = std::sregex_iterator();

std::cout << "Found "
    << std::distance(words_begin, words_end)
    << " words:\n";

for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
    std::smatch match = *i;
    std::string match_str = match.str();
    std::cout << match_str << '\n';
}

Found 2 words:
%1
%2

Yola
  • 18,496
  • 11
  • 65
  • 106