Replacing expression matches in Ruby with a table look up
<p>I have a large string of text and am using a regex to search for a certain delimited string key.  For example, I am interested in finding all occurrences of <code>[!v some_word]</code> in the text.  </p>

<p>I already have the regex to find the occurrences.  What I want to do is use the found <code>"some_word"</code> to look up a value in a database and replace the whole occurrence <code>"[!v some_word]"</code> with the value I found.  </p>

<p>Is there a way to do this as a one liner using regex's?  And as a catch...<code>"some_word"</code> might not be defined in the database, and in those cases I would want to ignore the match and just leave it.</p>

<p>My regex is: </p>

<pre><code>/(\[!v\s+([a-z\d_]+)\s*\])/
</code></pre>

<p>The text would contain something like: </p>

<pre><code>This proposal is created for [!v client_name].  As such [!v client_name] maintains personal rights...
</code></pre>

<p>The database would contain an entry keyed by <code>"client_name"</code> whose value might be <code>"Google"</code>.  So I want to be able to find all instances as marked by the <code>[!v...]</code>, determine the key (e.g., <code>client_name</code>), and then replace the <code>[!v...]</code> with the value associated with that key.  You could also assume that the value could be looked up through some association or even in a parameter array.</p>