0

I am comparing versions numbers delivered as a string.

I need to detect whether a version is a beta release or not, I want to check if beta, build or b is included in the last part of the version and I am trying to do this with a regex.

So far it works but as soon as there is a space in the version part it doesn`t.

Maybe someone has a better idea?

My regex so far:

(\d+)(beta|build|b(\d*))?

String examples:

v1.23beta125 --> ok

v1.25.458 beta 129 --> not recognized

Sample code:

private Boolean getBeta(String str) {

        boolean version = false;
        Pattern p = Pattern.compile("(\\d+)(beta|build|b(\\d*))?");

        Matcher m = p.matcher(str);

        if (!m.find()) {

            return version;

        } else
            version = true;

        return version;
    }
Simon
  • 1,691
  • 2
  • 13
  • 28

3 Answers3

0

You can use space as optional(0 or more)

Try this regex:

(beta|build|b)\s*(\d+)
karthik selvaraj
  • 426
  • 5
  • 12
0

I mean, all you want is just to find whether there is a b, beta or build in the version string, right? contains can do that. You don't need regexes :)

Well, you can also use this regex

build|beta|b

Check if find returns true.

You might also want to turn on case-insensitive option.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You can also use the following regex that is more restrictive:

v?\s*(\d+(\.\d*)*)+\s*(beta|build|b)\s*(\d+)

Tested on regex101: https://regex101.com/r/oLoVN3/2

you need to add a backslash \ for before each backslash \ in order to use it in your code.

Allan
  • 12,117
  • 3
  • 27
  • 51