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, ie 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}$
.
PS 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!
正则表达式应类似于re.compile(r'^\\d+(\\.\\d+){2,3}$')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.