title: String Pattern Matching/Finding/Counting/Replacing
body: <p>So this is a robust problem. I have a function which accepts 2 args (string_name, macros). Here it is so I can further explain.</p>
  
  <pre><code>function ParseStrings(string_name, macros)
      return my_table[string_name]
      -- All it does it returns the string_name's value
  end
  </code></pre>
  
  <p>The problem is that the second arg is a table, and if it's a table then in the string there are going to be various parts that have the format "String stuff $MACRO_KEY; more string text" and the content between the $ and ; is the key to look up in the macro table sent with it. Now anytime a value like that appears in the string there will always be a second arg that's a table, so no problems their. I need to be able to count up the number of instances of macros in a string and then replace each macro component with it's respective macros' table value. So here's how the func is called in this instance...</p>
  
  <pre><code>local my_table = {
      my_string = "My string content $MACRO_COMPONENT; more string stuff $MACRO_COMPONENT_SUB;$MACRO_COMPONENT_ALT;"
  }
  local macro = {
      MACRO_COMPONENT = "F",
      MACRO_COMPONENT_SUB = "Random Text",
      MACRO_COMPONENT_ALT = "14598"
  }
  
  function ParseStrings(string_name, macros)
      return my_table[string_name]
      -- All it does it returns the string_name's value
  end
  
  ParseStrings("my_string", macro)
  </code></pre>
  
  <p>So I am thinking:</p>
  
  <pre><code>string.gsub(my_table[my_string]:match("%b$;"):sub(2,my_table[my_string]:match("%b$;"):len() - 1)
  </code></pre>
  
  <p>but this is a long and overtly complex answer (AFAIK) and from my tests it only does 1 replacement (because the pattern is only found once) and that's doesn't work well if there are multiple instances in the string. So ideas?</p>