Search code examples
javaregexcasecamelcasing

Java Camel case apostrophe issue


Can anyone please help regarding camel casing strings that contain apostrophes, whereby I don't want the next letter after the apostrophe to be put to upper case.

My code reads from a txt file and then processes it as accordingly.

For example "MATTHEW SMITH" will be converted to "Matthew Smith" However "MATTHEW S'MITH" will be converted to "Matther S'Mith" when it should be "S'mith"

    public static String toCamelCase(String tmp){
    Pattern p = Pattern.compile(CAMEL_CASE_REG_EXP);
    Matcher m = p.matcher(tmp);
    StringBuffer result = new StringBuffer();
    String word;
    while (m.find()) 
    {
        word = m.group();

                    result.append(word.substring(0,1).toUpperCase()+word.substring(1).toLowerCase());

    }
    return result.toString();
}
public static final String CAMEL_CASE_REG_EXP = "([0-9]+)?([a-zA-Z]+)(\\')?(\\-)?(\\s)?";

Thanks in advance.


Solution

  • try this regex

    public static final String CAMEL_CASE_REG_EXP = "\\s*\\S+\\s*";
    

    it produces

    Matthew S'mith