Only these formats are accepted.
- 1.1.1
- 1.1.1-r
- 1.1.1-b
- 1.1.1-r1
- 1.1.1-b1
I wrote this code. What don't I like in it? I used parentheses and now I have two groups. In fact, I don't need to do anything with groups. And there might be some bugs as well.
package com.sandbox;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
checkVersion("10.22.5-r");
}
private static void checkVersion(String current) {
String regex = "([1-9]\\d*)\\.(\\d+)\\.(\\d+)-[b|r](?:[1-9]\\d*)*";
Pattern pattern = Pattern.compile(regex);
Matcher matcherCurrent = pattern.matcher(current);
System.out.println("Matches: " + matcherCurrent.matches());
System.out.println("The first digit: " + matcherCurrent.group(1));
System.out.println("The second digit: " + matcherCurrent.group(2));
System.out.println("The third digit: " + matcherCurrent.group(3));
// Compare and decide whether an update is necessary
}
}
Thank you for the answers you give me. I'll vote up as soon as I have enough reputation.
{0,1}
is the same as?
. \$\endgroup\$