neo
neo

Reputation: 425

Regular Expression for matching number greater than specified decimal number

I want to a match version numbers greater than 4.1. I constructed the following Regex for this

(([4-9]+\d*(\.((\*)|([2-9]+(\.((\*)|([0-9]+)))?)))?))

But it matches even '4' and does not match '5.1', '6.1' etc.

How to construct such a regular expression? Please help.

Upvotes: 4

Views: 5142

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149020

You could try this:

(4\.(1[0-9]*[1-9]|[2-9][0-9]*)|([5-9]|[1-9][0-9]+)(\.[0-9]+)?)

This will match:

  • 4. followed by either:

    • 1 followed by zero or more 0-9 and one or more 1-9
    • 2-9 followed by zero or more 0-9

    or

  • Either 5-9 or 1-9 followed by one or more 0-9
  • followed by an optional decimal point and zero or more 0-9

Depending on how this will be used, you might want to consider adding start / end anchors around your pattern so that no other characters will be allowed:

^(4\.(1[0-9]*[1-9]|[2-9][0-9]*)|([5-9]|[1-9][0-9]+)(\.[0-9]+)?)$

You can test it here.

Upvotes: 1

x4rf41
x4rf41

Reputation: 5337

try this:

([4-9]\.[2-9]\d*|[4-9]\.\d\d+|[5-9](\.\d+)?|\d\d+(\.\d+)?)

matches all versions above 4.1

Edit: fixed it for Versions without a dot

Upvotes: 4

Related Questions