user4576033
user4576033

Reputation: 9

remove whitespace in between non alphanumeric character

How do I remove whitespaces in between non-alphanumeric characters? For example

anti - C6 / 36 membrane antibodies 
D2 NS1 - P1 - specific antibodies

To

anti-C6/36 membrane antibodies 
D2 NS1-P1-specific antibodies

Upvotes: 0

Views: 102

Answers (3)

TheLostMind
TheLostMind

Reputation: 36304

Try regex like this :

public static void main(String[] args) {
    String s1 = "anti - C6 / 36 membrane antibodies";
    String s2 = "D2 NS1 - P1 - specific antibodies";
    String pattern = "\\s+(?=[^a-zA-Z0-9]+)|(?<=[^a-zA-Z0-9])\\s+";// replace all spaces either preceeded by or followed by a non-alphanumeric character
    System.out.println(s1.replaceAll(pattern, ""));
    System.out.println(s2.replaceAll(pattern, ""));
}

O/P:

anti-C6/36 membrane antibodies
D2 NS1-P1-specific antibodies

Upvotes: 0

vks
vks

Reputation: 67968

(?<=\W)[ ]+|[ ]+(?=\W)

Try this.Replace by empty string.See demo.

https://regex101.com/r/zB3hI5/11

For java it will be

(?<=\\W)[ ]+|[ ]+(?=\\W)

Upvotes: 1

anubhava
anubhava

Reputation: 784968

You can use this lookaround based regex for search:

(?<![\p{L}\p{N}]) +| +(?![\p{L}\p{N}])

And replace it by empty string.

RegEx Demo

In Java:

String repl = input.replaceAll( "(?<![\\p{L}\\p{N}]) +| +(?![\\p{L}\\p{N}])", "" );

(?<![\p{L}\p{N}]) | (?![\p{L}\p{N}]) means match a space if it is not followed by an alphanumeric or if not preceded by an an alphanumeric.

Upvotes: 0

Related Questions