Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex to remove space between numbers only

Tags:

java

regex

I have some strings like:

String a = "Hello 12 2 3 4 45th World!";

I wanna concatenate numbers as: "Hello 12234 45th World"

By trying a.replaceAll("(?<=\\d) +(?=\\d)", ""), I got the result as: "Hello 1223445th World".

Is there any way to concatenate only numbers, not number+th?

like image 735
Yang Qian Avatar asked Jun 10 '15 11:06

Yang Qian


1 Answers

(?<=\\d) +(?=\\d+(?:\\s|$))

You can try this.See demo.

https://regex101.com/r/nS2lT4/43

like image 86
vks Avatar answered Nov 02 '22 06:11

vks