0

I'm trying to write a regular expression for matching executable version number.
It can be provided in two ways:

  • MAJOR.MINOR.TINY
  • MAJOR.MINOR.TINY.BUILD_NUMBER

I do not need to parse these values, just check for match. So, I can: ^\d+[\.\d+]*$. Ok, but this regexp will match strings with 5 and more version parts, i.e 1.2.3.4.5. Also, it will match string like 1..2.3. It would be great to do something like ^[as many digits as you want and only one .]{3,4}$.

P.S. Of course, I can check for something like this ^(\d*)\.(\d*)\.(\d*)$ at first and the for ^(\d*)\.(\d*)\.(\d*)\.(\d*)$ if not matched. But maybe there is a better (smarter) way to do this.

I use re python module for checking the match:

import re

version_regexp = re.compile(r'^\d+[\.\d+]*$')
versions = ['dsvfsdf',
            '1.a.3',
            '1.2.4',
            '1.4..5.6',
            '0.3.1',
            'a.b.c.d',
            '1,2,3']
for i, sample in enumerate(versions):
    print('Sample[%d]: %s' % (i, True if version_regexp.match(sample) else False))

Thank you in advance!

0

1 Answer 1

6

The regex should be like re.compile(r'^\d+(\.\d+){2,3}$')

Sign up to request clarification or add additional context in comments.

1 Comment

D'ouh, realy simple. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.