I've asked this question before and got pointed at [1] where someone found that the default behaviour is greedy and they wanted non greedy search. Obviously someone didn't bother to read the question posted. The documented behaviour of the search string "\d+" is greedy. See the Boost documentation, [2], ("An expression followed by "+" can be repeated any number of times").
So a search of the target string "c12" with the regex "(\d+)" should result in a match of "12". This is my understanding, or perhaps misunderstanding. So the question [1] where the desired result is to stop at the first match, (non greedy) is the reverse of what I'm looking for, but thanks for pointing that question out, it affirms that the default behaviour should be greedy, as I want, but not what I get.
So the regex "\d+" applied to the string "c12" gives the result "2" which is non greedy behaviour. I'm looking for the greedy result as documented in [2] below. ("An expression followed by "+" can be repeated any number of times"). So I'm looking for a result of "12" the greedy answer and documented default behaviour of the '+' character.
I've previously posted an example in C with the output, but I'm sure we won't even get that far before we're pointed at [1] and the question closed again, but here goes:
#include <boost/regex.hpp>
#include <stdio.h>
#include <string>
int main(int argc, char **argv)
{
std::string token = "c12";
boost::smatch matches;
if (boost::regex_search(token, matches, boost::regex(".*(\\d+).*"))) {
int i = 0;
for (auto it = matches.begin(); it != matches.end(); it++) {
printf("match %ld:(%s)\n", i, (*it).str().c_str());
i++;
}
}
}
The above code gives the output:
- match 0:(c12)
- match 1:(2)
The first match is just the input string "c12" but the second match "2" is not as greedy as "\d+" would be expected to be?
There is another question [3] where someone is trying to match other characters around the digits in the target, but I'm only interested in the digits, and don't care what's before or after the digits so complete regex ".*(\d+).*" and test string of "c12".