title: Java 8 find and replace matching string(s)
body: <p>I am trying to find a string from <code>messages.properties</code> in an <code>errorMessage</code> and if the <code>errorMessage</code> has the string I need to replace it with corresponding value</p>
  
  <p>I have <code>messages.properties</code> as below</p>
  
  <pre><code>inv_date=INVOICE DATE
  inv_id=INVOICE NUMBER
  cl_id=CLIENT ID
  lf_matter_id=LAW FIRM MATTER ID
  inv_total_net_due=INVOICE TOTAL
  (inv_start_date|invoice start date)=BILLING START DATE
  (inv_end_date|invoice end date)=BILLING END DATE
  inv_desc=INVOICE DESCRIPTION
  units=LINE ITEM NUMBER OF UNITS
  discount_amount=LINE ITEM ADJUSTMENT AMOUNT
  total_amount=LINE ITEM TOTAL
  (charge_date|charge date)= LINE ITEM DATE
  acca_task=LINE ITEM TASK CODE
  acca_expense=LINE ITEM EXPENSE CODE
  acca_activity= LINE ITEM ACTIVITY CODE
  (tk_id|time keeper id)=TIMEKEEPER ID
  charge_desc=LINE ITEM DESCRIPTION
  lf_id=LAW FIRM ID
  (BaseRate|Rate|tk_rate)=LINE ITEM UNIT COST
  tk_level=TIMEKEEPER CLASSIFICATION
  cl_matter_id=CLIENT MATTER ID
  </code></pre>
  
  <p>My <code>errorMesage</code> can have any of the (left side)string and I need to replace it with right side string value</p>
  
  <p>Below are couple of sample error messages</p>
  
  <pre><code>String errorMesage1 = "Line 3 : Could not parse inv_date value" 
  String errorMesage2 = "Line : 1 BaseRate is a required field"
  </code></pre>
  
  <p>below is my method which conversts the error message</p>
  
  <pre><code>public static String toUserFriendlyErrorMessage(String message) {
      ResourceBundle rb = ResourceBundle.getBundle("messages");
      for(String key : rb.keySet()){
          String header = rb.getString(key);
          if(message.contains(key)) {
            return message.replaceAll(key, rb.getString(key));           
          }
      }
      return message;
  
  }
  </code></pre>
  
  <p>Below is the expected output:
  for errorMessage1 it works fine</p>
  
  <pre><code>System.out.println(toUserFriendlyErrorMessage(errorMessage1)); ==&gt; Line 3 : Could not parse INVOICE DATE value 
  </code></pre>
  
  <p>But for errorMessage2 its not working. It doesnt replace <code>BaseRate</code> with <code>LINE ITEM UNIT COST</code></p>
  
  <pre><code>System.out.println(toUserFriendlyErrorMessage(errorMessage2)); ==&gt; Line : 1 BaseRate is a required field
  </code></pre>
  
  <p>Is there a way to find the occurance of multiple strings and replace it with its corresponding value?</p>
  
  <p>For example: Find <code>(BaseRate|Rate|tk_rate)</code> and replace the string with <code>LINE ITEM UNIT COST</code></p>
  
  <p>Also am wondering can this method simplified further in java 8?</p>