Python Forum
Please support regex for version number (digits and dots) from a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please support regex for version number (digits and dots) from a string
#1
Hello Community,

I have the following string

"SYS"."M_SYSTEM_OVERVIEW" ('System', 'Version', '', '2.00.050.00.1592305219 (fa/hana2sp05)')
I need the version information
2.00.050.00.1592305219
from that string.

I tried several things via regex but all failed. One of my ideas was to use the first part until the version number as a own group.
m = re.search('("SYS"."M_SYSTEM_OVERVIEW" \(\'System\', \'Version\', \'\', \')(\S+)(.+)', line)
Version =  m.group(2)
This lead to an AttributeError: 'NoneType' object has no attribute 'group'
How should I design patterns to get this?
Also I am not sure when to escape in that string and when not.

Can someone please assist.

Regards

--Christian
Reply
#2
What's the purpose for parsing it? There are a couple of tools that can be used to compare "version-like" strings. 'packaging.version' is probably the standard parser.
Reply
#3
Hello bowlofred,

I am analyzing several files with different kind of information with a Python script. One part is to get this version information.

Regards

--Christian
Reply
#4
i usually avoid regex unless absolutely required. Here it is not because you can parse the string through process of elimination. And based on the fact that your regex string contains this in every file.

s = '''"SYS"."M_SYSTEM_OVERVIEW" ('System', 'Version', '', '2.00.050.00.1592305219 (fa/hana2sp05)')'''
version = s.split(',')[3].split(' ')[1].strip("'")
print(version)
outputs from elimination to final desired output. From why each index was selected from a certain split character
Output:
[email protected]:~$ python3 test2.py ['"SYS"."M_SYSTEM_OVERVIEW" (\'System\'', " 'Version'", " ''", " '2.00.050.00.1592305219 (fa/hana2sp05)')"] [email protected]:~$ python3 test2.py '2.00.050.00.1592305219 (fa/hana2sp05)') [email protected]:~$ python3 test2.py ['', "'2.00.050.00.1592305219", "(fa/hana2sp05)')"] [email protected]:~$ python3 test2.py '2.00.050.00.1592305219 [email protected]:~$ python3 test2.py 2.00.050.00.1592305219
The only thing that has to be added is obtaining the original string section from the file which could be different based on what the content of the file contains. You would have to find something unique in that string in comparison to parse out the rest of the file. Or for example if it has 1000 commas in the file, the index could be 98 instead of 3, etc.
Reply
#5
Hello metullburr,

thank you for your answer and taking time.

This one works.

I have slept a few nights over this. I found a solution with regex.

import re

s = '''"SYS"."M_SYSTEM_OVERVIEW" ('System', 'Version', '', '2.00.050.00.1592305219 (fa/hana2sp05)')'''
m = re.search('(Version)(\D+)(\S+)', s)
test1 = m.group(1)
test2 = m.group(2)
test3 = m.group(3)

print("string = ", s)
print("test1: ", test1)
print("test2: ", test2)
print("test3: ", test3)
This one gives me also the Version string I am looking for. I will see which solution I will use in my script.
I will mark my thread as solved.

Regards

--Christian
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo How can I use 2 digits format for all the number? plumberpy 6 692 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Regex: a string does not starts and ends with the same character Melcu54 5 699 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,734 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 1,107 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 923 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Help getting a string out of regex matt_the_hall 4 888 Dec-02-2020, 01:49 AM
Last Post: matt_the_hall
  Digits of a number 1234 2 763 Nov-27-2020, 05:43 PM
Last Post: perfringo
  Single digits seem to be greater than double digits Frosty_GM 4 1,034 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Make an array of string number in a List polantas 5 1,435 May-27-2020, 07:18 AM
Last Post: buran
  [regex] Good way to parse variable number of items? Winfried 4 1,189 May-15-2020, 01:54 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

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