首页 > 解决方案 > Regular expression to extract software version from the given text?

问题描述

I am looking for a regular expression that can extract the software version from a string. Few samples look like

1) 'ABCD 2.3.4' 
o/p: 2.3.4

2) 'ANDROID 4.4 KIT KAT SELFIX'
o/p: 4.4

3) '0.1.0-D-20170309.1502'
o/p: 0.1.0

4) 'CONTIXO-LA703-20180915-v1.0'
o/p: 1.0

My regex condition failing and unable to satisfy all these conditions. Here is my regex:

''.join(re.findall("[0-9.]", txt))

This can only satisfy the first two cases. How can I extract the software version from all the above cases?

Note: My old question was not specific and was closed without a solution. I deleted the question and added a new one. Hope this one meets the standard.

标签: pythonpython-3.xregex

解决方案


In Python3: Use following, in case there are multiple matches present get the first one.

import re
re.findall('\d+(?:\.\d+)+', '3) \'0.1.0-D-20170309.1502\'')[0]
'0.1.0'
re.findall('\d+(?:\.\d+)+', '2) \'ANDROID 4.4 KIT KAT SELFIX\'')[0]
'4.4'

Explanation: Adding detailed explanation for above.

\d+          ##Looking for digits 1 or more occurrences here.
(?:\.\d+)+   ##Starting a non-capturing group which looks for a literal DOT followed by one or more
             ##digits occurrences AND one or more occurrences of this non-capturing group with `+` it will match.


As per The fourth bird's nice comment, you could try following too:

(?<![\d-])\d+(?:\.\d+)+

推荐阅读