-3

Note: This question is not about available libraries which parse version strings or about how to write regex pattern in general. I have to feed a pattern into an API call so using another library is not an option for me.

I need to match a version string like e.g. "9.3" or "12.5" against a minimum version number "11.2". I.e. "9.9" or "9.10" must not match while "11.2", "11.10" or "20.0" should.

In Python re is there a reasonable way to accomplish this? Using approaches like this one lead to an expression like this one:

r"^(([2-9]\d|1[2-9])\.\d{1,}|11\.([2-9]|\d{2,}))(\.\d+)*$"

which is hard core to read and zero flexible.

In 2020 isn't there an easier or more generic way to deal with decimal numbers (not digits) in regular expressions in Python?

Maybe expression generators which take a more abstract description and generate expressions?

frans
  • 8,868
  • 11
  • 58
  • 132

1 Answers1

0

To match all integers and decimal numbers greater than or equal to 11, use this regular expression:

^[1-9][1-9](\d+)?\.?\d*

  • ^ Beginning of string.
  • [1-9] First digit must be in the range 1 to 9 (inclusive).
  • [1-9] Same for second digit.
  • (\d+)? Might be followed by more digits.
  • \.? Might be followed by a decimal.
  • \d* Might be followed by digits in the fractional part.
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43
  • The full expression would be `r"^(([2-9]\d|1[2-9]).\d{1,}|11.([2-9]|\d{2,}))(.\d+)$"` - and this is what my question is about. I was asking whether there is an easier or even generic way to describe a pattern which can be easily described on an abstract level. – frans Jul 27 '20 at 08:53
  • According to https://regex101.com/r/E6WcWH/1 your expression doesn't work for your test cases. No idea what you're trying to do. – GirkovArpa Jul 27 '20 at 15:28
  • you're right - there was a `*` missing at the end of the line - I took an example which matches 11.2.3 (with an additional subversion), see https://regex101.com/r/QRF2gl/1 – frans Jul 28 '20 at 07:20
  • This doesn't match `20.0` – Toto Jul 28 '20 at 08:46
  • accoding to https://regex101.com/r/QRF2gl/4 it does.. – frans Aug 07 '20 at 12:30