Reputation:
I want to get the version number for some string in php. Here is the sample code
<?php
function getVersion($str) {
preg_match("/.*((?:[0-9]+\.?)+)/i", $str, $matches);
return $matches[1];
}
print_r(getVersion("ansitl1-isam-6.0 1.0 9.7.03 418614 +"));
print_r(getVersion("ams-ef 9.6.06ef4 - 394867"));
?>
for input string ansitl1-isam-6.0 1.0 9.7.03 418614 +
output should be 9.7.03
for input string ams-ef 9.6.06ef4 - 394867
output should be 9.6.06
How to achieve this?
Upvotes: 1
Views: 348
Reputation: 18490
If the pattern is always num.num.num preceded by a space.
(?<= )\d+\.\d+\.\d+
See this demo at Regex101 or a PHP demo at tio.run
There is not much Regex magic used here, just a lookbehind to check, there is a space before.
Instead it can also be done by a caturing group and getting $out[1]
like in this demo.
Upvotes: 1