Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Camel Case to Lower Hyphen in Java when there are contiguous capitals

Tags:

java

regex

guava

I have the string "B2BNewQuoteProcess". When I use Guava to convert from Camel Case to Lower Hyphen as follows:

CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,"B2BNewQuoteProcess");

I get "b2-b-new-quote-process".

What I am looking for is "b2b-new-quote-process"...

How do I do this in Java?

like image 920
user27478 Avatar asked Nov 30 '17 19:11

user27478


3 Answers

Edit

To prevent - at the beginning of a line, use the following instead of my original answer:

(?!^)(?=[A-Z][a-z])

Code

See regex in use here

(?=[A-Z][a-z])

Replacement: -

Note: The regex above doesn't convert the uppercase characters to lowercase; it simply inserts - into the positions that should have them. The conversion of uppercase characters to lowercase character occurs in the Java code below using .toLowerCase().

Usage

See code in use here

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        final String regex = "(?=[A-Z][a-z])";
        final String string = "B2BNewQuoteProcess";
        final String subst = "-";
        
        final Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(string);
        
        // The substituted value will be contained in the result variable
        final String result = matcher.replaceAll(subst);
        
        System.out.println("Substitution result: " + result.toLowerCase());
    }
}

Explanation

  • (?=[A-Z][a-z]) Positive lookahead ensuring what follows is an uppercase ASCII letter followed by a lowercase ASCII letter. This is used as an assertion for the position. The replacement simply inserts a hyphen - into the positions that match this lookahead.
like image 91
ctwheels Avatar answered Oct 26 '22 12:10

ctwheels


Camel case Lower hyphen Should have been
B2BNewQuoteProcess b2-b-new-quote-process b2b-new-quote-process
BaBNewQuoteProcess ba-b-new-quote-process
B2NewQuoteProcess b2-new-quote-process
BABNewQuoteProcess b-a-b-new-quote-process bab-new-quote-process

So:

  • Digits after a capital count as capital too
  • With N+1 "capitals" the first N capitals form a word

A repair of the wrong result would be:

String expr = "b2-b-new-quote-process";
expr = Pattern.compile("\\b[a-z]\\d*(-[a-z]\\d*)+\\b")
    .matcher(expr).replaceAll(mr ->
        mr.group().replace("-", ""));

This searches between word boundaries (\b) a sequence of letter with any digits, followed by a repetition of hyphen plus letter with any digits.

like image 2
Joop Eggen Avatar answered Oct 26 '22 12:10

Joop Eggen


METHOD FOR VERSIONS BELOW JAVA 8

Use this method to convert any camel case string. You can select any type of separator.

private String camelCaseToLowerHyphen(String s) {
    StringBuilder parsedString = new StringBuilder(s.substring(0, 1).toLowerCase());
    for (char c : s.substring(1).toCharArray()) {
      if (Character.isUpperCase(c)) {
        parsedString.append("_").append(Character.toLowerCase(c));
      } else {
        parsedString.append(c);
      }
    }
    return parsedString.toString().toLowerCase();
  }
}

Original code taken from: http://www.java2s.com/example/java-utility-method/string-camel-to-hyphen-index-0.html

like image 2
A.Casanova Avatar answered Oct 26 '22 10:10

A.Casanova