CamelCase in Groovy, Java

Example for versions Groovy 1.7, Sun Java 6

This example uses Java regular expressions. A regular expression [a-zA-Z]+ describes any contiguous sequence of letters (in any case), surrounded with non-letter characters or ends of string. Classes Pattern and Matcher allow to create this regular expression and extract from the string all fragments which match it. For each such fragment, its first letter is converted to upper case, and the rest of it — to lower case. Finally, the resulting word is appended to StringBuffer variable which accumulates the result.

import java.util.regex.*;

public class CamelCase {
    public static void main(String[] args) {
      try {
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        Pattern p = Pattern.compile("[a-zA-Z]+");
        Matcher m = p.matcher(br.readLine());
        StringBuffer result = new StringBuffer();
        String word;
        while (m.find()) {
            word =;
            result.append(word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase());
      } catch (Exception e) {
        System.err.println("An error occured while reading input string.");