So I have text from which I'd like to extract some key-value pairs. I want to do this in the most efficient way possible, so I was thinking of regex. But I don't understand how to say "if this key exists - take its value, and if not, continue taking other existing key-pair values".
So let's say I have this text, and I want to extract only Value3 and Value4:
Placeholder1
String: Key1=Value1, Key2=Value2, Key3=Value3, Key4=Value4
Placeholder2
String: Key1=Value1, Key2=Value2, Key3=Value3, Key4=Value4
For this run I just want the first appearance, i.e. right after Placeholder1. So I have something like this:
Placeholder1\s*.*Key3=([a-zA-Z0-9 -]*).*Key4=([a-zA-Z0-9 -]*)
Which works and gets me Group 1 = Value3, Group 2 = Value4. Excellent.
However, if I have the following string without Key3=Value3:
Placeholder1
String: Key1=Value1, Key2=Value2, Key5=Value5, Key4=Value4
My regex of course doesn't work, even though I want it to get me Key4. So I thought that putting the groups with a ()? would work, so that if it exists it will take it, and if not - move on:
Placeholder1\s*.*(Key3=([a-zA-Z0-9 -]*))?.*(Key4=([a-zA-Z0-9 -]*))?
However adding the ? returns me nothing from the original text where both key-value pairs exist. When I remove the ? it will work again, but not when Key3 is missing.
So how do I build a regex that will take the maximum number of key-value pairs that exist in the text?
PS - The key-value pairs can appear with/without other key-value pairs between them.