1

I want to convert a component version (for example 2.6.0) to this form G02R06C00. I think it's possible with regex and pattern with Java but I don't found how. I have seen examples to match a string against a pattern (in this case the regex would be G\d\dR\d\dC\d\d I guess), but I don't find how to convert a string to other string with a pattern. Thanks in advance.

What I have done is:

String gorocoVersion = version.replaceAll("(\\d*)\\.(\\d*)\\.(\\d*)", "G$1R$2C$3");

that produces G2R6C0, it's missing yet to add 0 to match 2 digits. So, I have to split input string before and I can't anymore use replaceAll, that's why I was looking for more clever option that automatically add 0 before simple digit too according to the output pattern G\d\dR\d\dC\d\d, like we can find in word or excel

Something that works with snapshot version but it's very ugly :

/**
 * @return version in G00R00C00(-SNAPSHOT) format.
 */
private String formatVersion() {
    String gorocoVersion = "";
    if (StringUtils.isNotBlank(version)) {
        String[] versionTab = version.split("\\.");
        String partG = String.format("%02d", Integer.parseInt(versionTab[0]));
        String partR = String.format("%02d", Integer.parseInt(versionTab[1]));

        String partC = versionTab[2];
        String snapshotSuffix = "-SNAPSHOT";
        if (partC.endsWith(snapshotSuffix)) {
            partC = String.format("%02d",
                    Integer.parseInt(partC.substring(0, partC.indexOf('-')))) + snapshotSuffix;
        } else {
            partC = String.format("%02d", Integer.parseInt(partC));
        }

        gorocoVersion = "G" + partG + "R" + partR + "C" + partC;
    }
    return gorocoVersion;
}
12
  • I have edited my question. Commented Apr 1, 2020 at 10:00
  • I want to know if it exists a function to convert a string to another string who matches a pattern. I have the input string "2.6.0", I have the output pattern "G\d\dR\d\dC\d\d" but is there a function to create an output string matching this pattern ? that is to say G02R06C00 Commented Apr 1, 2020 at 10:06
  • Ah, you are looking for string format. Or, match and capture your numbers and then replace (with String.replaceAll) using backreferences in the replacement to point to captured numbers. Commented Apr 1, 2020 at 10:07
  • See stackoverflow.com/questions/37681277/… Commented Apr 1, 2020 at 10:10
  • yes I had seen replaceAll but I was looking something more clever, which could replace a string expression automatically towards an output pattern and also add automatically 0 before simple digit, thanks to the output pattern "G\d\dR\d\dC\d\d". According to your response, wHat I have done is : String gorocoVersion = version.replaceAll("(\\d*)\\.(\\d*)\\.(\\d*)", "G$1R$2C$3"); that produces G2R6C0, it's missing yet to add 0 to match 2 digits Commented Apr 1, 2020 at 13:03

2 Answers 2

0

Try this method:

private static String formatVersion(String version) {
    Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(-SNAPSHOT)*$");
    Matcher matcher = pattern.matcher(version);
    if (matcher.find()) {
        return String.format("G%02dR%02dC%02d", Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)));
    } else {
        throw new IllegalArgumentException("Unsupported version format");
    }
}

Output for the version param value 2.6.0:

G02R06C00
Sign up to request clarification or add additional context in comments.

3 Comments

Great ! Thanks, I'm trying to adapt your code in order to work with -SNAPSHOT at the end too
I've updated the expression, now it will work and with the -SNAPSHOT suffix. If you want to add or to do something else with this suffix, use the matcher's group 4.
yes I had updated my code in other message as you can see. Thank you Ilya :)
0

Thanks a lot Ilya, here my final code which accepts SNAPSHOT and also x.y versions :

private String formatVersion(String version) {
    if (StringUtils.isNotBlank(version)) {
        Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+)\\.?(\\d+)?(-\\w*)?");
        Matcher matcher = pattern.matcher(version);
        if (matcher.find()) {
            return String.format("G%02dR%02dC%02d%s", Integer.parseInt(matcher.group(1)),
                    Integer.parseInt(matcher.group(2)),
                    matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : 0,
                    matcher.group(4) != null ? matcher.group(4) : "");
        }
    }
    return version;
}

With this code 2.6 gives G02R06C00 and 2.6.1-SNAPSHOT gives G02R06C01-SNAPSHOT, perfect :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.