I am trying to extract a version number from a string using regular expression. The version number is in the format "D.D.Dc", where 'D' are digits (can be one or more instances) and 'c' is an optional alphabet character, surrounded by white spaces on either side.
The string I want to extract it from is something like:
FOO 5.1.7d BAR 5.0.2 2019/06/18
The regular expression I'm using is:
\s(\d+)\.(\d+)\.(\d+)([a-zA-Z])?\s
Below is the the code I'm using.
static regex FWVersionFormat{ R"(\s(\d+)\.(\d+)\.(\d+)([a-zA-Z])?\s)" };
auto matches = cmatch{};
if (regex_search(strVersion.c_str(), matches, FWVersionFormat))
{
int maj = 0, min = 0, maint = 0, build = 0;
if (!matches[1].str().empty()) maj = strtol(matches[1].str().c_str(), nullptr, 10);
if (!matches[2].str().empty()) min = strtol(matches[2].str().c_str(), nullptr, 10);
if (!matches[3].str().empty()) maint = strtol(matches[3].str().c_str(), nullptr, 10);
if (!matches[4].str().empty()) build = matches[4].str().c_str()[0] - ('a' - 1);
return{ maj, min, maint, build };
}
This works fine if there is only one match in the version string but the issue is that the regex_search() is putting the second instance of the version into the matches ("5.0.2").
I want to be able to only extract the first match. Is there any way to do this using regex?
Aucun commentaire:
Enregistrer un commentaire