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;
}
Ilya Lysenko
  • 1,772
  • 15
  • 24
Nono
  • 43
  • 2
  • 8
  • I have edited my question. – Nono Apr 01 '20 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 – Nono Apr 01 '20 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. – Wiktor Stribiżew Apr 01 '20 at 10:07
  • See https://stackoverflow.com/questions/37681277/how-to-format-a-number-with-regular-expression – Wiktor Stribiżew Apr 01 '20 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 – Nono Apr 01 '20 at 13:03
  • 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 – Nono Apr 01 '20 at 13:21
  • @WiktorStribiżew can you reopen the post please because I have to add comments here... – Nono Apr 01 '20 at 14:06
  • @WiktorStribiżew I have editted my post and it's answerable... – Nono Apr 01 '20 at 14:21
  • I just am not sure you need regex at all to manipulate a `1.2.3` kind of string.I also see your input can be `1.2.3-SNAPSHOT`, right? And this suffix must be kept if it is there, right? – Wiktor Stribiżew Apr 01 '20 at 14:28
  • yes, it's right. My solution above works without regex but I look for something more powerful, short and clever, maybe with a regex or other function I don't know. – Nono Apr 01 '20 at 14:43
  • It makes no sense using regex here, I removed the tag. – Wiktor Stribiżew Apr 01 '20 at 14:48
  • ok, that's why I was thinking maybe it's possible to use regex in the opposite way we common use, that is to say not to find if a string match a pattern but construct a string against a pattern. BUt it seems it does not exist :D – Nono Apr 01 '20 at 14:55

2 Answers2

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
Ilya Lysenko
  • 1,772
  • 15
  • 24
  • 1
    Great ! Thanks, I'm trying to adapt your code in order to work with -SNAPSHOT at the end too – Nono Apr 01 '20 at 15:54
  • 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. – Ilya Lysenko Apr 01 '20 at 20:29
  • yes I had updated my code in other message as you can see. Thank you Ilya :) – Nono Apr 02 '20 at 07:47
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 :)

Nono
  • 43
  • 2
  • 8