Azizi Musa
Azizi Musa

Reputation: 1029

Replace multiple non-digit char to 1 non-digit char

I am working on app that read weight value from weighing indicator. The output from the indicator are contains with symbols, non digit char and also a number. I just want to extract the number. I have already turn non-digits and symbols into several pipes using regex \D. Then I wanted to turn this string

||||||||||1234||||||||||||||1234||||||||||||||1234||||||||||||||1234||||||||||||||1234||||

into

|1234|1234|1234|1234

How could I possibly do that?

Upvotes: 0

Views: 32

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

You could try a regex replacement:

String input = "||||||||||1234||||||||||||||1234||||||||||||||1234||||||||||||||1234||||||||||||||1234||||";
String output = input.replaceAll("\\|+", "|").replaceAll("\\|$", "");
System.out.println(output);  // |1234|1234|1234|1234|1234

Upvotes: 3

Related Questions