Juli
Juli

Reputation: 159

Regex for parsing version number

How can I write a regex for parsing version numbers. I want to match numbers like: 1.000, 1.0.00, 1.0.0.000 but not integers 1, 10,100

Upvotes: 0

Views: 3966

Answers (4)

Avinash Raj
Avinash Raj

Reputation: 174696

I think you want something like this,

(?:(\d+\.[.\d]*\d+))

OR

(?:(\d+\.(?:\d+\.)*\d+))

DEMO

>>> import re
>>> str = 'foobar 1.000, 1.0.00, 1.0.0.000 10 100 foo bar foobar'
>>> m = re.findall(r'(?:(\d+\.(?:\d+\.)*\d+))', str)
>>> m
['1.000', '1.0.00', '1.0.0.000']

Upvotes: 3

Andy
Andy

Reputation: 50550

This doesn't directly answer your question, but it is making an assumption that you want this functionality to compare different versions of an application.

You can do this using distutils.version (the docs are empty, but we can find function documentation in the source )

A couple examples to help understand what we are doing:

>>> from distutils.version import LooseVersion, StrictVersion
>>> LooseVersion("1.0.1") < LooseVersion("1.0.2")
True
>>> StrictVersion("1.0.1") < StrictVersion("1.0.2")
True
>>> LooseVersion("1.0.10") < LooseVersion("1.0.1")
False

What's the difference between LooseVersion and StrictVersion? With LooseVersion you can compare "real world" versions like this:

>>> LooseVersion("2.1-rc1") < LooseVersion("2.2")
True

Whereas StrictVersion doesn't allow alphabetic characters

>>> StrictVersion("2.1-rc1") < StrictVersion("2.2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\distutils\version.py", line 40, in __init__
    self.parse(vstring)
  File "C:\Python27\lib\distutils\version.py", line 107, in parse
    raise ValueError, "invalid version number '%s'" % vstring
ValueError: invalid version number '2.1-rc1'

Upvotes: 2

jforberg
jforberg

Reputation: 6752

Clarification: I'm assuming you want to parse the numbers, not just match them.

Why use regexes when a simple split will work just fine?

'1.3.4.*'.split('.') # => ['1', '3', '4', '*']

If you want to ensure that there is at least one dot in the string, check the array length to ensure it is larger than 1.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

Try this regex:

^(\d+\\.)?(\d+\\.)?(\\*|\d+)$

Upvotes: 1

Related Questions