I have a string like this;
ab cd 1234567 1234567 ef gh 1234567 1234567 ij kl - - - -
I want the output to look like this;
abcd 1234567 1234567 efgh 1234567 1234567 ijkl - - - -
How to achieve this? Currently I am using the following and it doesn't work.
result = result.trim().replaceAll("(\\w)(\\s+)([\\.,])", "");
Thanks.
To remove all whitespaces between ASCII letters you may use
result = result.trim().replaceAll("(?<=[A-Za-z])\\s+(?=[A-Za-z])", "");
Or, to match any Unicode whitespaces between any Unicode letters, you may use
result = result.trim().replaceAll("(?<=\\p{L})\\s+(?=\\p{L})", "");
Note that in Java, you must prepend the pattern with (?U)
if you want \s
to match any Unicode whitespaces (the shorthand character classes are not Unicode-aware by default in Java, they are in Android).
See the regex demo
Details
(?<=\\p{L})
- a positive lookbehind that matches a location inside a string that is immediately preceded with any Unicode letter\\s+
- 1+ whitespaces(?=\\p{L})
- a positive lookahead that matches a location inside a string that is immediately followed with any Unicode letter.