camel Case To Snake Case with regex - Java java.lang

Java examples for java.lang:String Case

Description

camel Case To Snake Case with regex

Demo Code


//package com.java2s;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern CAMEL_CASE_TO_SNAKE_CASE_PATTERN = Pattern
            .compile("((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))");

    public static String camelCaseToSnakeCase(String camelCase) {
        return CAMEL_CASE_TO_SNAKE_CASE_PATTERN.matcher(camelCase)
                .replaceAll("_$1").toLowerCase();
    }/*  ww  w  .ja v  a 2  s  .c  o m*/
}

Related Tutorials