javaandroidregexspace

How to remove space only between words and not digits or special characters using Regex?


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.


Solution

  • 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