Q:

Extract correct combination of digits from string

I am working on a method which extracts the correct version number from a String (for later comparisation if there is a new version).
The String which is being provided unfortunatly has no fixed pattern and can have multiple combinations of digits.
Below code parses the version number but if the string includes for example build 50124 this number is pasted after the extracted version combination which I do not want.
It could also be that combos in the string are possible like for example v1.12 [build452] or  v1.32.856 (build 8754) but I already took care of that by substringing the parts as of [ or (.
The method in question:
private String extractVersion(String str){
        str = str.substring(str.lastIndexOf('v'),
                str.indexOf('[') != -1 ? str.indexOf('[') : str.length()).trim();
        str = str.substring(str.lastIndexOf('v'),
                str.indexOf('(') != -1 ? str.indexOf('(') : str.length()).trim();
        return str.replaceAll("[^0-9?!\\.]", "");
    }

I tested it on some examples but unfortunatly the result is not what I want, sometimes digits are pasted behind the actual version number, I refer to the logcat output.
Is there a 'better' regex or another way I can use to improve the method, so it extracts the correct version number?
Thank you for your help.
..
NewVersion = tempWatchList.get(i);
  Log.w("Versionnr", extractVersion(NewVersion));
  Log.e("Versionname", NewVersion);
..

Logcat outout:
/Versionnr: 5.131 //should be 5.13
/Versionname: somename v5.13b1
/Versionnr: 2.0..4 //should be 2.0
/Versionname: another name 2 v2.0.exp.4
/Versionnr: 18.01 //should be 18.0
/Versionname: somename v18.0-ALPHA1
/Versionnr: 7.2.42221..3634639 //should be 7.2.4222
/Versionname: another name v7.2.4222-1.L.3634639
/Versionnr: 5.0.220170112 //should be 5.0.2
/Versionname: somename v5.0.2 build 20170112
/Versionnr: 4.4.0.201701124401 //should be 4.4.0
/Versionname: another name v4.4.0.20170112b4401

A:

You can use this to extract versions:
Pattern p = Pattern.compile("[0-9]+([.][0-9]+){1,2}");
Matcher m = p.matcher(NewVersion);
m.find();
Log.w("Versionnr", m.group());

