0

FYI, It is not duplicate please remove this tag.

I have tried packaging.version.Version it does not work on 1.0.2h and 192.168.0.3.

Question

I am getting versions of different libraries/binaries in a list. And this list includes some garbage values of version along with correct one. I want to write a python code that can separate correct ones from the version list.

Please suggest a logic or python library which can do the following

Psuedo Code

Correct_verList = []

verList = ['2.6.36', '1.2.dfgdf', '1.sdfsdgsdsf', '3.0/0+3*/1,-/0/0,//O*K1', 'so.802', '5.0(side)', '6.2*20hm' , '192.168.0.222', '1.33', '0.97a', '1.0.2h', '2.4.2b3']

#Psuedo Code
for ver in verList: 
    if ver is the correct version:
         Correct_verList.append(ver)

print(Correct_verList)

Expected Output

Correct_verList = [1.33, 0.97a, 2.6.36, 1.0.2h, 2.4.2b3]
Umair
  • 336
  • 1
  • 16
  • @DeveshKumarSingh dear, it is different, as it is to find that if a string is version or not. and u suggested the version comparison. – Umair May 29 '19 at 06:58
  • If you look at the answer again `version.Version("1.3.xy123")` checks if a string is a valid version or not – Devesh Kumar Singh May 29 '19 at 06:59
  • `packaging.version.Version` doesn't seem to allow `1.0.2h`. – tfw May 29 '19 at 07:17
  • `packaging.version.Version` says `192.168.0.12` is a version, in actual, it is not a version. – Umair May 29 '19 at 07:21
  • Can't add an answer because of the duplicate but if your requirement is very specifically a number followed by a period followed by a combination of a letters and numbers optionally followed by a period and a combination of a letters and numbers you could solve it with a regular expression, something like `import re; result = [x for x in verList if re.fullmatch('\d+\.\w+(\.\w+)?', x)]` – tfw May 29 '19 at 07:42
  • @tfw this code is working for the given list. But it is too specific to the given list. It says `2.sfufdf`, `2.34e.34e` and `2.0.xxxdd` are versions but these are not. AND seems like I can't do anything regarding duplicate it is up to @DeveshKumarSingh – Umair May 29 '19 at 08:08
  • Your requirements are still not very clear to me, but maybe the expression `(\d+\.){1,2}\d+\w*` could solve it instead. Also, as the answerer suggested, I recommend you read up on the documentation for RegEx and experiment a bit with it, e.g. through [regex101](https://regex101.com/) or [RegExr](https://regexr.com/). – tfw May 29 '19 at 09:08

1 Answers1

0

What you are looking for is called regular expression. This link provide you documentation on python's regular expression.

This regular expression could do the job : '(([0-9]+[.]){1,2}([0-9]*([0-9]|[a-z]){0,2}[0-9]+)'

What you could do is to use the findall() method and test if the result is the same string you tested. The code would be like (I have not tested it) :

import re
Correct_verList = []

verList = ['2.6.36', '3.0/0+3*/1,-/0/0,//O*K1', 'so.802', '5.0(side)', '6.2*20hm' , '192.168.0.222', '1.33', '0.97a', '1.0.2h', '2.4.2b3']

for ver in verList:
    a=re.findall('(([0-9]+[.]){1,2}([0-9]*([0-9]|[a-z]){0,2}[0-9]+)',ver)
    if len(a)!=0:
       if a[0][0]==ver:
           Correct_verList.append(ver)
print(Correct_verList)

TUI lover
  • 542
  • 4
  • 16
  • That regex won't match all of his requirements. Note that letters can be included in the last segment. – tfw May 29 '19 at 06:58
  • @tfw you are right, in last segment character is also possible – Umair May 29 '19 at 06:59
  • @Umair corrected. Is it matching your requirements? – TUI lover May 29 '19 at 07:15
  • @TUIlover there is an error in the code. – Umair May 29 '19 at 07:46
  • @Umair corrected. Still, you didn't explain why `192.168.0.12` shouldn't be in the list and my code does not exclude it – TUI lover May 29 '19 at 08:02
  • @TUIlover `192.168.0.12` is IP address. FYI, a version has maximum of three segments. – Umair May 29 '19 at 08:06
  • @Umair it should work now – TUI lover May 29 '19 at 08:13
  • Thanks @TUIlover, It is working. Can you please limit the characters appearing in the last segment to maximum 2. like `1.2.dfgdf` and `1.sdfsdgsdsf` are not versions. – Umair May 29 '19 at 08:19
  • @Umair You must use this regular expression : `'(([0-9]+[.]){1,2}([0-9]*([0-9]|[a-z]){0,2}[0-9]+)'` instead. I strongly recommend you to read [this](https://docs.python.org/2/library/re.html). The 7.2.1. section is short and is very useful, as regular expression are implemented in numerous languages in that way. If you think the question is answered, please up-vote the answer and mark the question as answered. – TUI lover May 29 '19 at 08:27
  • @TUIlover I have done it finally....`RegEx` is an amazing library. Thanks, I will do it. – Umair Jun 07 '19 at 21:07