0

I was wondering if there is any guarantee about what does std::regex_search match when there are multiples match possibilities.

For example consider this simple regex that look for two positive integers: ([0-9]+).+([0-9]+).

If I search in this string <foo>123</foo><bar>456</bar>789<foobar> the two numbers matched could be:

  • 123 and 456

  • 456 and 789

  • 123 and 789

  • 2 and 56

  • 4 and 5

  • a lot of other possibilities...


To get a guarrantee that only full numbers are matched I can change the regex to >([0-9]+)<.+>([0-9]+)< and now the two numbers matched could be:

  • 123 and 456

  • 456 and 789

  • 123 and 789

Is there any guarantee about which of theses 3 possibilities will be returned by std::regex_search ?

If not, how to get the guarantee to get for example the first and shortest match (123 and 456) ?


    std::string str = "<foo>123</foo><bar>456</bar>789<foobar>";

    std::regex expr(">([0-9]+)<.+>([0-9]+)<");
    std::smatch match;

    if(!std::regex_search(str, match, expr))
        puts("Not found");

    printf("First number: %s\nSecond one: %s\n", match.str(1).c_str(), match.str(2).c_str());

The output I get is 123 and 789 but I expect 123 and 456.

J. Doe
  • 13
  • 3

0 Answers0