Raj Raichand
Raj Raichand

Reputation: 97

How to remove dash (-) from string with regex except between numbers & words in java?

I am trying to remove dashes, underscores & extra spaces from a string.

Below is the code I tried :

public class Hello {

   public static void main(String args[]) {


          String blogName = "ABCD            __________________________________________________                      Size:      1234-5678 BAR                     8956-7896       ----------           CAR                     8756-2365";

          String nameWithProperSpacing = blogName.replaceAll("\\s+", " ").replaceAll("-", "").replaceAll("_", ""); 

          System.out.println( nameWithProperSpacing );
    }
}

I don't want it to remove them from between words & numbers, though. For example my current code changes 8956-7896 to 89567896.

The output I want :

ABCD Size:1234-5678 BAR 8956-7896 CAR 8756-2365

Upvotes: 0

Views: 782

Answers (5)

lolacoco
lolacoco

Reputation: 101

You will need search with lookahead and lookbehind

      String nameWithProperSpacing = blogName
              .replaceAll("(?<![a-zA-Z0-9])[-_](?![a-zA-Z0-9])", "") ///No -_ not inside a word
              .replaceAll("\\s+", " "); /// only 1 space between words

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

You can play with non-word boundaries \B to remove them, but to do that you have to remove underscores first:

String nameWithProperSpacing = blogName.replaceAll("\\s+", " ").replaceAll("_+", "").replaceAll("\\B-+|-+\\B", "");

Upvotes: 0

Catalina Chircu
Catalina Chircu

Reputation: 1574

Another solution:

String nameWithProperSpacing = blogName.replaceAll("([^A-Za-z0-9])(\\-|\\s|_)+", "$1 ");

Upvotes: 0

Saurav Sahu
Saurav Sahu

Reputation: 13974

What you are trying to achieve can be simply done by (Keep single dash, remove multiple dashes):

blogName.replaceAll("\\-(\\-)+","");

Upvotes: 1

Jokkeri
Jokkeri

Reputation: 1055

Try this, not pretty but works with your example:

.replaceAll("\\s+", " ")
            .replaceAll("--", "").replaceAll("__", "").replaceAll(" _", "")
            .replaceAll("_ ", "").replaceAll(" -", "").replaceAll("- ", "")
            .replaceAll("  ", " ").trim();

Upvotes: 0

Related Questions