View Single Post
12-02-14, 07:17 AM   #12
Digital_Utopia
A Flamescale Wyrmkin
 
Digital_Utopia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2012
Posts: 110
Originally Posted by Dridzt View Post
This would be a nice opportunity for some community effort in coming up with a sane pattern for version checking.

https://docs.oracle.com/javase/7/doc...x/Pattern.html

Test version strings (eg. 'v2.08-3a') here and submit pattern proposals
http://ocpsoft.org/tutorials/regular...-regex-tester/
some php code that should parse most versions - including the example.
only reason why php, is that it's quicker to play with - as opposed to compiling and UI stuff.

Code:
<?
//pattern - checks for the presence of a "v", with or without space,
//then checks for numbers, dashes and dots - and captures those. 
//and finally, recognizes the posibility of whitespace, and looks for any letters following it. 
$pat = '/[vV\s]*([0-9\-\.]+)[\s]*([a-zA-Z]?)/';
//test string
$str="v2.08-3a";
//do the match
preg_match($pat,$str,$match);
//replace dashes with dots
$newstr = str_replace("-",".",$match[1]);
//split it by the dots
$sstring = explode(".",$newstr);
//init vars
$ver ="0";
$subver ="0";
//test if there are actually any dots
if(count($sstring)>1)
{
	//if so, combine the first two (i.e. 2.08)
	$ver=strval($sstring[0]).".".strval($sstring[1]);
	//check for moar dots!
	if(count($sstring)>2)
	{
		//loop through the rest, appending any additional parts
		for($i=2; $i < count($sstring);$i++)
		{
			$ver.=strval($sstring[$i]);
		}
	}
}else{
	//otherwise, just set it to the first value. 
	$ver=$sstring[0];
}
//check if we have a letter;
if(count($match)>2)
{
	//if so, drop it low, then get the char value of it - 97 in this case
	//char values go up in alphabetical order, but 'A' and 'a' need to be treated the same. 
	$subver=ord(strtolower($match[2]));
}

//and output the version (2.083) and the subversion (97)
echo "Version: ".$ver."<br>";
echo "SubVersion: ".$subver;
// 2.08.4 will be a newer version, as will 2.08.3b, and so forth.

?>
I'd be willing to take on more "challenges" to put it to the test of course.
__________________

Last edited by Digital_Utopia : 12-02-14 at 07:22 AM.
  Reply With Quote