0

Need help in parsing a string, where it contains values for each attribute. below is my sample string...

Type=<Series VR> Model=<1Ac4> ID=<34> conn seq=<2>

from the above, I have to generate the attribute values are below.

Type=Series VR
Model=1Ac4
ID=34
conn seq=2

I am new to using regex. any help is appreciated. thanks

marc
  • 181
  • 2
  • 16

2 Answers2

1

This script will extract key, value from the string:

import re

s = 'Type=<Series VR> Model=<1Ac4> ID=<34> conn seq=<2>'

for k, v in re.findall(r'([^=]+)=<([^>]+)>\s*', s):
    print('{}={}'.format(k, v))

Prints:

Type=Series VR
Model=1Ac4
ID=34
conn seq=2

EDIT: You can extract key,values to dictionary and then access it via .get():

import re

s = 'Type=<Series VR> Model=<1Ac4> ID=<34> conn seq=<2>'

d = dict(re.findall(r'([^=]+)=<([^>]+)>\s*', s))

print(d.get('Model', ''))
print(d.get('NonExistentKey', ''))

Prints:

1Ac4

 
Andrej Kesely
  • 81,807
  • 10
  • 31
  • 56
  • thanks for quick solution Andrej. looking for any way to search for a specific key and extract it own value. like search for Model in the string and return only its value '1Ac4'... like not using loop and extract value based on key and if not present, wanted to make it to null. – marc Oct 21 '20 at 21:46
  • @marc I updated my answer. You can make a dict with the regex and access it via `[]` or `.get()` with default value (if key is not present). – Andrej Kesely Oct 21 '20 at 21:54
  • 1
    thanks for the solution – marc Oct 21 '20 at 23:26
0

Try this:

r'([^=]+)=<([^>]+)>'

This works as follows:

  1. ([^=]+) matches a group of any characters aren't = and is at least one character long.
  2. =< matches those characters literally
  3. ([^>]+) matches another group of any characters that aren't > and is at least one character long.

To match a specific key and 'extract' it's value, try this (example shown is 'Model'):

r'Model=<([^>]+)>'

This now only has one grouping, the ([^>]+) grouping that matches the value contained within <>. This could be generalized for any key, like so: f'{key}=<([^>]+)>'

Collin Heist
  • 1,032
  • 1
  • 4
  • 16
  • thanks for the explanation Collin. is there any way to search for a specific key and extract it own value. like search for Model in the string and return only its value '1Ac4' – marc Oct 21 '20 at 21:45
  • Sure, rather than using the `([^=]+)` grouping, literally match that string. Such as: `r'Model=]+)>'` I will put that in my answer. – Collin Heist Oct 21 '20 at 21:47
  • thanks for the solution – marc Oct 21 '20 at 23:25