title: Python with regex to replace strings recursively
body: <p>I've tried different ideas for this but haven't been successful yet. Hence this post.</p>
  
  <p>Some background: I am trying to decipher a Makefile that might include variables from a separate file. I've managed to read all the variables in Makefile and also its includes successfully into a python dictionary. But now I find that each of the values essentially reference other variables that are part of the dictionary. What I want to do is to unroll all the values in the dictionary to have text that are independent of other key/value pairs. This definitely involves recursion (IMHO) but I am very interested in hearing any other suggestions.</p>
  
  <p>Note that not all variables might have a value associated with it. In this case, replace the key with a NULL string.</p>
  
  <p>Now for some code to demonstrate what is said above:</p>
  
  <p>Let a list of key,value pair be </p>
  
  <p>*A = -L${F} ${B} ${D},</p>
  
  <p>*B = -L/myhome,</p>
  
  <p>*F = /usr/lib</p>
  
  <p>I want to write a python script (possibly with regex) to recursively replace the values matching '${XXX}' with the corresponding key until there are no more values available that match the prescribed pattern (i.e., everything is unrolled). Since D doesn't have a value associated with it, I want the value of A to eventually be (for example)</p>
  
  <p>*A = -L/usr/lib -L/myhome </p>
  
  <p>Thanks in advance. Any help would be much appreciated.</p>