A_Elric
A_Elric

Reputation: 3568

Java Regex to split numbers with unknown number of spaces

I have a string of numbers that are a little weird. The source I'm pulling from has a non-standard formatting and I'm trying to switch from a .split where I need to specify an exact method to split on (2 spaces, 3 spaces, etc.) to a replaceall regex.

My data looks like this:

23574     123451    81239   1234    19274  4312457     1234719

I want to end up with

23574,xxxxx,xxxxx,xxxx 

So I can just do a String.split on the ,

Upvotes: 4

Views: 5019

Answers (4)

gersonZaragocin
gersonZaragocin

Reputation: 1202

I understand this problem as a way to obtain integer numbers from a string with blank (not only space) separators.

The accepted solution does not work if the separator is a TAB \t for instance or if it has an \n at the end.

If we define an integer number as a sequence of digits, the best way to solve this is using a simple regular expression. Checking the Java 8 Pattern API, we can find that \D represents any non digit character:

\D  A non-digit: [^0-9]

So if the String.split() method accepts a regular expression with the possible separators, it is easy to send "\\D+" to a trimmed string and get the result in one shot like this.

String source = "23574     123451    81239   1234    19274  4312457     1234719";
String trimmed = source.trim();
String[] numbers = trimmed.split("\\D+");

It is translated as split this trimmed string using any non digit character sequence as a possible separator.

Upvotes: 0

tcannon91
tcannon91

Reputation: 217

String pattern = "(\s+)";
Pattern regex = Pattern.compile(pattern);
Matcher match = r.matcher(inputString);
match.replaceAll(",");
String stringToSplit = match.toString();

I think that should do it for you. If not, googling for the Matcher and Pattern classes in the java api will be very helpful.

Upvotes: 2

gtgaxiola
gtgaxiola

Reputation: 9331

I will use \s Regex

This is its usage on Java

String[] numbers = myString.split("\\s+");

Upvotes: 13

user180100
user180100

Reputation:

final Iterable<String> splitted = Splitter.on('').trimResults().omitEmptyStrings().split(input);
final String output = Joiner.on(',').join(splitted);

with Guava Splitter and Joiner

Upvotes: 3

Related Questions