java - simplified regex solution for version with format <0-999>.<0-999>.<0-999> -


before everything,

  • i have gone through several other regex related questions , came solution looks there might simpler solution
  • i beginner regex
  • i using java execute server side

version should of format "< major >.< minor >.< beta >" either of them having range <0-999>

my desired behaviour follows

0             -   0.0.0 1             -   1.0.0 000.0.0       -   0.0.0 ..9           -   0.0.9 00007         -   7.0.0 090876        -   validate false 9.00098000.00 -   validate false 9.0900000.00  -   9.09.0 -13.0.4       -   validate false 1a.0.4b       -   validate false 

my solution follows

if (stringutils.isblank(version)) {  //set error } else {     if (!this.version.trim().matches("\\d+") && !(this.version.trim().matches("^-+"))  && !(this.version.trim().matches("^+"))) {         string[] versionsplit = this.version.split("\\.");         // in format <major>.<minor>.<beta> major version,         // leading zeroes should removed         versionsplit[0] = versionsplit[0].trim().replacefirst("^0+(?!$)", "");         if (versionsplit[0].length() == 0) {             // if major version left blank add 0             versionsplit[0] = "0";         }         (int = 1; < versionsplit.length; i++) {             // minor , beta versions, trailing zeroes should             // removed             versionsplit[i] = versionsplit[i].trim().replaceall("0*$", "");             if (versionsplit[i].length() == 0) {                 versionsplit[i] = "0";             }         }         this.version = stringutils.join(versionsplit, ".");         if (versionsplit.length < 3) {             // comply standard format <major>.<minor>.<beta>             (int = versionsplit.length; < 3; i++) {                 version += ".0";             }         }     } else { //set error }  if(< no error check > && !(version.matches("\\d(\\d(\\d)?)?(\\.\\d(\\d(\\d)?)?(\\.\\d(\\d(\\d)?)?)?)?"))) { // set error     } } 

tell me if isn't complicated solution, glad let be. want code readable next person.

please ask if requirement unclear. please don't frustrated if not respond not online

thanks in advance.

edit

i understand format checker @ end more accurate in below way.

if(< no error check > && !(version.matches("(?:[0-9]{1,3}\\.){2}[0-9]{1,3}$"))) { // set error     } 

basically need better understanding of regular expressions , how work in java.

here code example corresponding output, explanations follow below:

    list<string> versions = arrays.aslist("0", "1", "000.0.0", "100.2.3","1.2.3", "..9", "00007", "090876", "9.00098000.00", "9.0900000.00", "-13.0.4", "1a.0.4b", "1.2");     pattern pattern = pattern.compile("^0*([1-9][0-9]{0,2})?(?:\\.([0-9]{0,3}?)0*(?:\\.([0-9]{0,3}?)0*)?)?$");     (string v : versions) {         matcher matcher = pattern.matcher(v);         boolean matches = matcher.matches();         integer groups = matcher.groupcount();         system.out.print(v + " evaluates " + matches);         // groups 3, because empty string matches         if (matches) {             string major = matcher.group(1);             if (major == null || major.isempty()) {                 major = "0";             }             string minor = matcher.group(2);             if (minor == null || minor.isempty()) {                 minor = "0";             }             string beta = matcher.group(3);             if (beta == null || beta.isempty()) {                 beta = "0";             }             system.out.println(" ---> " + major + "." + minor + "." + beta);         } else {             system.out.println();             // error handling         }     } 

this results in following output:

0 evaluates true ---> 0.0.0 1 evaluates true ---> 1.0.0 000.0.0 evaluates true ---> 0.0.0 100.2.3 evaluates true ---> 100.2.3 1.2.3 evaluates true ---> 1.2.3 ..9 evaluates true ---> 0.0.9 00007 evaluates true ---> 7.0.0 090876 evaluates false 9.00098000.00 evaluates false 9.0900000.00 evaluates true ---> 9.09.0 -13.0.4 evaluates false 1a.0.4b evaluates false 1.2 evaluates true ---> 1.2.0 

everything revolves around pattern , regular expression compiled from. https://docs.oracle.com/javase/7/docs/api/java/util/regex/pattern.html?is-external=true wonderful resource started (and way beyond started) regular expressions in java.

you should capturing , not capturing groups , quantifiers, both greedy , reluctant.

in regex i've provided above ([1-9][0-9]{0,2})? capturing group major version. must start numeric other 0 , can have 2 following numeric characters (this time 0 included). leading nulls discarded because of leading 0* in regex - observe how not inside capturing group. greedy quantifier ? @ end means group optional. next group optional, , furthermore non capturing group, i.e. won't accessible after match complete. inside discard zeros , capture 2 more groups, of in turn 1 optional, using same principles.

edit: modified answer accept version strings x.y author suggested.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -