Java - Write code to split camel case strings into normal strings with spaces using regex.

Requirements

Write code to split camel case strings into normal strings with spaces using regex.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "AnbdDef Book2s.com";
        System.out.println(splitCamelCase(s));
    }//from   w w w .j a v  a2s . c om

    /**
     * This method splits camel case strings into normal strings with spaces.
     * 
     * @param s
     *            is the string that should be transformed.
     * @return returns the transformed string
     */
    public static String splitCamelCase(String s) {
        return s.replaceAll(String.format("%s|%s|%s",
                "(?<=[A-Z])(?=[A-Z][a-z])", "(?<=[^A-Z])(?=[A-Z])",
                "(?<=[A-Za-z])(?=[^A-Za-z])"), " ");
    }
}

Related Exercise