-3

I want to extract the version numbers using python regular expression.

The below string is stored in "str1" variable

show.sh
 {
   "sys_0_num" :  {
   "rel_num": 2.3,
   "version": 14891
 },
   "sys_1_num" :  {
   "rel_num": 2.3,
   "version": 14891
 }

  "cha_num" :  {
  "rel_num": 2.3,
  "version": 571,
  "model":1487
 }

}

I want to extract the version number from each component i.e.,

sys_0_num version: 14891
sys_1_num version: 14891
cha_num version": 571

I tried this below linecode, but unable to get the desired output (the above string is stored in str1 variable):

 Sample code 
 output2 = re.findall(r'sys_0_num\[version]:\s*(\d+)',str1)[0]
Tranbi
  • 11,407
  • 6
  • 16
  • 33
Shishira
  • 1
  • 3

1 Answers1

0

You can use pattern "(\w+)"\s*:\s*\{.*?("version"\s*:\s*\d+) which has two capturing groups: one for identifier string i.e. "(\w+)"\s*:\s*\{ , another for version i.e. ("version"\s*:\s*\d+), along with re.DOTALL flag for multi-line match.

>>> text=''' {
   "sys_0_num" :  {
   "rel_num": 2.3,
   "version": 14891
 },
   "sys_1_num" :  {
   "rel_num": 2.3,
   "version": 14891
 }
  "cha_num" :  {
  "rel_num": 2.3,
  "version": 571,
  "model":1487
 }
}'''
>>> re.findall('"(\w+)"\s*:\s*\{.*?("version"\s*:\s*\d+)', text, re.DOTALL)

[('sys_0_num', '"version": 14891'), ('sys_1_num', '"version": 14891'), ('cha_num', '"version": 571')]

You can later just join and print them:

>>> print('\n'.join(' '.join(match) for match in re.findall('"(\w+)"\s*:\s*\{.*?("version"\s*:\s*\d+)', text, re.DOTALL)))

sys_0_num "version": 14891
sys_1_num "version": 14891
cha_num "version": 571
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • Thank you for your solution. If I want to print without the list and tuple I,e., [()]--> 'sys_0_num': "version": 14891'. How can it be done? is it possible if I append [0] at the end? this isn't working hence the question. – Shishira Aug 11 '21 at 07:55
  • You can just join them. – ThePyGuy Aug 11 '21 at 07:56
  • oh thank you so much for your solution sir. It helped me a lot. – Shishira Aug 11 '21 at 08:04
  • if we want to extract a floating point number--> in the above eg, rel_num is 2.3, will (\d*) is the right thing to do? or this one \d+\.\d+ – Shishira Aug 11 '21 at 08:50
  • Yeah, `\d+\.\d+` will work, but only if all the numbers are floating number, if some of them are integer, and some of them are floating, then you can use `(\d+(?:\.\d+)?)`, it will match both the integer numbers and floating point numbers. – ThePyGuy Aug 11 '21 at 08:55
  • Also, you have used .join(' '.join(match) here. Is there any particular material/link on this to learn? – Shishira Aug 11 '21 at 09:02
  • You can take a look at [Common string operations](https://docs.python.org/3/library/string.html) – ThePyGuy Aug 11 '21 at 09:06
  • Sir, also do you have any idea about this-> https://stackoverflow.com/questions/68731828/when-clicking-a-button-in-a-window-using-pygtk-how-can-i-print-the-result-on-th/68732906#68732906 – Shishira Aug 11 '21 at 11:21
  • Hello sir, can you help me in getting the solution for this question https://stackoverflow.com/questions/68893317/how-to-extract-particlar-message-from-a-vast-displayed-output-using-python-regul – Shishira Aug 23 '21 at 13:27