regex to extract version info I have written a small Python function to extract version info from a string.
Input is of format:


v{Major}.{Minor}[b{beta-num}]


Eg: v1.1, v1.10b100, v1.100b01
Goal is to extract Major, Minor and beta numbers.

import re
def VersionInfo(x):
    reg = re.compile('^v([0-9]+)([.])([0-9]+)(b)?([0-9]+)?$')
    return reg.findall(x)
if __name__ == '__main__':
    print(VersionInfo('v1.01b10'))
    print(VersionInfo('v1.01'))
    print(VersionInfo('v1.0'))


Output:

[('1', '.', '01', 'b', '10')]
[('1', '.', '01', '', '')]
[('1', '.', '0', '', '')]


Question is:


Is it sufficient to cover all cases?
Better regex?
^v([0-9]+)([.])([0-9]+)(b)?([0-9]+)?$


Not much to say but:


seems a bit strange to capture the . since that's the only value that could be there. Also [.] could be written mode simply as \..
also seems a bit strange to capture the b, unless v1.1b is specifically allowed. And you're trying to find that last digit group even if b isn't present - not a bug, but could be made clearer.


To address the second point, make the whole beta part optional:

^v([0-9]+)\.([0-9]+)(?:(b)([0-9]+)?)?$


If beta with no number isn't allowed, this can be simplified (and drop capturing b also in this case):

^v([0-9]+)\.([0-9]+)(?:b([0-9]+))?$


(The (?:...) construct is a non-capturing group.)