7

I try to write a regular expression in order to:

  1. Add white spaces between each capital letter.
  2. Remove all numbers.

I have the text: ClassNameOne839, and I want to get the text: Class Name One

There is a library function which do it? or any regular expression?

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
Or Smith
  • 3,556
  • 13
  • 42
  • 69

4 Answers4

21

You can use a combination of replaceAll() calls like this:

String text = "ClassNameOne839";
String cleanText = text.replaceAll("\\d+", "").replaceAll("(.)([A-Z])", "$1 $2");

This first removes all numbers, and then adds a space before all upper-case letters that are not at the beginning of the String. $1 and $2 are references to the first and second groups of the regex.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • 3
    In case you want to preserve numbers and Google brought you here, try this: String cleanText = text.replaceAll("(.)([A-Z0-9]\\w)", "$1 $2"); Output: "Class Name One 839" – Zeek Aran Nov 23 '18 at 15:51
1
org.apache.commons.lang.StringUtils.splitByCharacterTypeCamelCase

Does almost everything you need ;) Try this one:

cleanText = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(in.replaceAll("\\d+", "")), " ");

Should be much faster than regexps.

michali
  • 401
  • 2
  • 11
0

The following regular expression [A-Z][a-z]+(?=[A-Z\s\b0-9]) Would match each individual capitalized word followed by another capital letter, a space character or digit. Simply print each result in a row with a space behind it and you're set

Edit: This is ofcourse given that your capitalized names don't contain weird characters, the use would be something like this:

Matcher matcher = Pattern.compile("[A-Z][a-z]+(?=[A-Z\s\b0-9])").matcher(input);
while(matcher.find()) {
   string = string + matcher.group();
}
Sytem.out.println(string);
Bart Enkelaar
  • 695
  • 9
  • 21
0
String result = "ClassName399".replaceAll("\\d", "").replaceAll("([A-Z])", "$1 ");

The first expression will detect all integers in the string (\\d) and delete them, the second looks for any capital letter (using the range operator []) and then takes what you found $1 and adds a space after it.

Please refer to Java Pattern class for more info on regular expressions.

Rossiar
  • 2,416
  • 2
  • 23
  • 32