Java Regex Version Validate parseVersion(final String versionString)

Here you can find the source of parseVersion(final String versionString)

Description

parse Version

License

Open Source License

Declaration

public static Integer parseVersion(final String versionString) 

Method Source Code

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static Integer parseVersion(final String versionString) {
        // Our version format is as follows:
        // MajorVersion.MinorVersion.BuildVersion
        // JJ.NN.BB
        final String PERIOD_REGEX = "\\.";
        final int SIG_DIGITS = 100;

        String[] versionSections = versionString.split(PERIOD_REGEX);

        // strip trailing alpha characters, if any, from the build number
        // example 0.0.30A -> 0.0.30
        if (versionSections.length == 3) {
            final String BUILDINFO_REGEX = "([0-9]*)(.*)"; // capture groups
            final Pattern pattern = Pattern.compile(BUILDINFO_REGEX);
            final Matcher matcher = pattern.matcher(versionSections[2]);
            if (matcher.find() && matcher.groupCount() >= 2) {
                versionSections[2] = matcher.group(1); // first group
            }/*from  w  w w .j a va2s. co m*/
        }

        // /** LOG DISABLED Log.d(TAG, "parseVersion(" + versionString +
        // ") versionSections[" + versionSections.length + "]"); **/

        int iTmp = 0;

        for (int i = 0, len = versionSections.length; i < len; i++) {
            // /** LOG DISABLED Log.d(TAG, "parseVersion[" + i + "](" +
            // versionSections[i] + ")"); **/

            try {
                Integer tmpI = Integer.parseInt(versionSections[i]);
                // Increment the section (because each section can have two
                // significant digits
                iTmp *= SIG_DIGITS;
                // Add the current value to it
                iTmp += tmpI.intValue();
            } catch (NumberFormatException e) {
                /**
                 * LOG DISABLED Log.d(TAG,
                 * "Numeric Format Exception, when parsing (" +
                 * versionSections[i] + ")", e);
                 **/
            }
        }

        // /** LOG DISABLED Log.d(TAG, "parseVersion(" + versionString +
        // ") became: " + iTmp); **/

        return iTmp;
    }
}

Related

  1. parseVersion(String s, String groupId, String artifactId)
  2. parseVersion(String version, int defaultRevision)
  3. parseVersion(String version, String currentVersion)
  4. parseVersionNamespace(String versionNamespace)