Reputation: 97
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
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
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
Reputation: 1574
Another solution:
String nameWithProperSpacing = blogName.replaceAll("([^A-Za-z0-9])(\\-|\\s|_)+", "$1 ");
Upvotes: 0
Reputation: 13974
What you are trying to achieve can be simply done by (Keep single dash, remove multiple dashes):
blogName.replaceAll("\\-(\\-)+","");
Upvotes: 1
Reputation: 1055
Try this, not pretty but works with your example:
.replaceAll("\\s+", " ")
.replaceAll("--", "").replaceAll("__", "").replaceAll(" _", "")
.replaceAll("_ ", "").replaceAll(" -", "").replaceAll("- ", "")
.replaceAll(" ", " ").trim();
Upvotes: 0