0

I am trying to find the regex that can find two numbers in the string, first number consists of 10 digits, second of 58 (9091968376 and 8887918192818786347374918784939277359287883421333333338896). These two numbers can be in any place of the string. Here is what I have so far.

#include <iostream>
#include <regex>

void dump_regex_matches(const std::cmatch& match)
{
    std::cout << "Matches:\n";

    for (std::size_t i = 0; i < match.size(); ++i)
        std::cout << i << ": "  << match.str(i) << '\n';
}

int main()
{
    std::regex regex(R"(.*(\d{10}).*(\d{58}))");
    std::string searchStr = "var a=0,m,v,t,z,x=new Array('9091968376','8887918192818786347374918784939277359287883421333333338896','778787','949990793917947998942577939317'),l=x.length;while(++a<=l){m=x[l-a];}";
    std::cmatch match;
    if (std::regex_match(searchStr.c_str(), match, regex))
    {
        std::cout << "We got match!\n";
        dump_regex_matches(match);
    }

    return 0;
}

It works if I do not have quotes surrounding the numbers but with them it don't. Appreciate any help.

Oleg
  • 1,027
  • 1
  • 8
  • 18

1 Answers1

0

regex_match checks for a full match. Add .* at the end to allow anything after:

std::regex regex(R"(.*(\d{10}).*(\d{58})).*");

Or use regex_search instead of regex_match:

int main()
{
    std::regex regex(R"((\d{10}).*(\d{58}))");
    std::string searchStr = "var a=0,m,v,t,z,x=new Array('9091968376','8887918192818786347374918784939277359287883421333333338896','778787','949990793917947998942577939317'),l=x.length;while(++a<=l){m=x[l-a];}";
    std::cmatch match;
    if (std::regex_search(searchStr.c_str(), match, regex))
    {
        std::cout << "We got match!\n";
        dump_regex_matches(match);
    }

    return 0;
}
Anonymous
  • 11,748
  • 6
  • 35
  • 57