How to replace a regexp group with a post proceed value?

Posted by Pentium10 on Stack Overflow See other posts from Stack Overflow or by Pentium10
Published on 2010-06-03T14:01:16Z Indexed on 2010/06/03 14:04 UTC
Read the original article Hit count: 314

Filed under:
|
|

I have this code to

public static String ProcessTemplateInput(String input, int count) {
        Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}");
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            String newelem=SelectRandomFromTemplate(matcher.group(1), count);
        }
        return input;
    }

Input is:

 String s1 = "planets {Sun|Mercury|Venus|Earth|Mars|Jupiter|Saturn|Uranus|Neptune}{?|!|.} Is this ok? ";

Output example:

String s2="planets Sun, Mercury. Is this ok? ";

I would like to replace the {} set of templates with the picked value returned by the method. How do I do that in Java1.5?

© Stack Overflow or respective owner

Related posts about java

Related posts about regex