-1

I read this dictionary from a file as a string:

string = '{"key1":"value1", "key2":"value2"}'

I don't know which values key1 and key2 can assume, I need to access the value of the last pair written on the string (in this case 'value2'). So converting it into a python dictionary is out of the question since it would probably change the order.

I've tried something like:

value = re.search(r'"(.*?)":"(.*?)"', string)

But I don't know how to access the .group(2) of the last match.

Hyperion
  • 2,515
  • 11
  • 37
  • 59

1 Answers1

1

You can do this :

import re
output = re.findall(".*\"[a-zA-Z0-9]+\"[\w]*:[\w]*\"(.+)\"[\w]*}", string)

Output :

>>> re.findall(".*\"[a-zA-Z0-9]+\"[\w]*:[\w]*\"(.+)\"[\w]*}", string)
['value2']
Jarvis
  • 8,494
  • 3
  • 27
  • 58