Reputation: 425
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
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
5-9
or 1-9
followed by one or more 0-9
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
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