Python Forum
Extracting Version Number from a String
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting Version Number from a String
#1
Question 
Hi,
I would like to be able to extract the version number (nimbers and ,) from a string.
I can get the result but only if I use 1 type of delimeter
I have noticed that sometimes the delimeter changes so would like to have multiple delimeters options.
For example

import re
text = 'mawk/kinetic,now 1.3.4.20200120-3.1 amd64 [installed,automatic]'
m = re.search(',now (.+?)-', text)

print(m) 
Output:
<re.Match object; span=(12, 32), match=',now 1.3.4.20200120-'>
print(m.group(0) 
Output:
,now 1.3.4.20200120-
print(m.group(1))
Output:
1.3.4.20200120
In this case m.group(0) contains the answer I need as the delimiter was - but sometimes it can be a + or : as the final delimeter
Equally the first delimeter might be ,now 1:

For info the string is generated by apt list --installed | grep -i mawk (in this case)

So I am getting in a bit of a pickle as I know I should be able to have multiple start delimeters and multiple end delimters,
I assume there must be a simpler way of doing this rather than a series of if statements searching to see what is the start and end delimeter in a string and then calling the correct re.search code.
Regex is really powerfull and a language unto itself but I've not got much spare capacity in my onbaord mental ROM anymore.
Any advice gratefully received.
Thanks and kind regards,
jB Cool
buran write Apr-05-2023, 10:11 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
(Apr-03-2023, 05:45 PM)britesc Wrote: In this case m.group(0) contains the answer I need as the delimiter was - but sometimes it can be a + or : as the final delimeter
Then you should use:
m = re.search(',now (.+?)[-+:]', text)
Does that give the results you need?
tester_V likes this post
Reply
#3
Thumbs Down 
Sorry for the delayed response. Not very well.
I have managed to use your code with a bit extra to achieve what I required.
Thank you very much. Cool
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extracting x from '(x)' as an integer number Hwolff1962 3 890 Mar-23-2023, 02:50 PM
Last Post: Hwolff1962
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,231 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,533 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,431 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,210 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  Extracting year from a string using strptime and datetime builtin Drone4four 3 8,733 Aug-11-2020, 12:02 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 3,124 May-27-2020, 07:18 AM
Last Post: buran
  line number of first and second occurance of string in a file mdalireza 1 1,849 Nov-18-2019, 09:55 AM
Last Post: perfringo
  How can i judge 1st string position is correct number christing 3 2,427 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020