Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The regex code in Java to separate a string using commas, one or more spaces, and the boundaries of digits and letters, while excluding dots would be:

String regex = "[,\\s]+(?![\\d\\.]+)\\b(?<![\\d\\.]+)";

Explanation:

  • [,\\s]+ - matches one or more comma or whitespace characters
  • (?![\\d\\.]+) - negative lookahead, to exclude any digits or dots immediately following the separator
  • \\b - word boundary, to match the boundary between a word character (letter or digit) and a non-word character (space or comma)
  • (?<![\\d\\.]+) - negative lookbehind, to exclude any digits or dots immediately preceding the separator

Example usage:

String input = "ABC, 123 DEF 567, HIJ.890";
String[] parts = input.split("[,\\s]+(?![\\d\\.]+)\\b(?<![\\d\\.]+)");
System.out.println(Arrays.toString(parts));

Output:

[ABC, 123, DEF, 567, HIJ.890]