title: Most efficient way in groovy to replace many tokens
body: <p>I have <em>n</em> random tokens in a string and i want to use closures to replace each of this values with a return value from a method.</p>
  
  <p>e.g.</p>
  
  <blockquote>
    <p>String theString = "Hi, this is \$randomEmail and this is a rand
    string: \$randomString and this is  a random \$randomTelephoneNumber"</p>
  </blockquote>
  
  <p>Then what i would like to do is call each applicable method based on the roke to replace that string with an actual value - e.g. for the email address token ($randomEmail) then I want to call the following method;</p>
  
  <pre><code>String randomEmailAddress() {
          return new StringBuilder(RandomStringUtils.random(6)).append("@").append(RandomStringUtils.random(6)).append(".com").toString();
      }
  </code></pre>
  
  <p>Was hoping to use closures the string replace function but a little stuck on the next steps..</p>
  
  <pre><code> Map&lt;String,Closure&gt; randomStrings = [randomEmail:{randomEmailAddress()},randomUserName:{randomString(8),randomTelephoneNumber:{randPhoneNumber()}}]
  </code></pre>
  
  <p>Then call the replace function on the string and have each token in turn be replaced with the correct random value. </p>
  
  <p>Note: that there are many base string that I need to manipulate (the example only shows one of them) and I may or may not have all the values present in the file that are in the map (model) at any one time.</p>
  
  <p>i know i can do this with an if statement but defeats the purpose of learning closures:-).</p>
  
  <p>thanks in advance, </p>
  
  <p>Ian.</p>