View Javadoc

1   package com.aurea.maven.plugins.sonic.utils;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   public class RegexUtil {
7   
8   
9     /**
10     * Find #{text} in the string and replaces any occurrences of str in text by replacement. The entire match is then
11     * replaced by the result.
12     * 
13     * @param text
14     * @return string replaced version of string
15     */
16    public static String applyReplaceFunction(String text, String str, String replacement) {
17  
18      Pattern pattern = Pattern.compile("#\\{([^\\}]*)\\}");
19      Matcher matcher = pattern.matcher(text);
20  
21      StringBuffer result = new StringBuffer();
22      while (matcher.find()) {
23        String argument = matcher.group(1);
24        matcher.appendReplacement(result, argument.replace(str, Matcher.quoteReplacement(replacement)));
25      }
26      matcher.appendTail(result);
27  
28      return result.toString();
29    }
30  
31  }