0

I have a string and I need to remove these symbols: -- + [ ] { } ( ) \ /

For example:

    String clean = "This \ is / an example. This -- is + an [(example)].";

    clean = clean.replaceAll("[/[()/]]", "");
    clean = clean.replaceAll("/-/-", "");

    clean = clean.replaceAll("\\/","");
    clean = clean.replaceAll("\\\\", " ");
    clean = clean.replaceAll("\\+", "");

    return clean.replaceAll("[ ]+", " ").trim();

My output should be: This is an example. This is an example.

My code does not remove everything I need and also I would like to know if there is a shorter way to do this.

--

Just some particularities I should mention: - should be removed only if there are two together. / should be replaced by a whitespace. I'm going to try to adapt your solutions here. Thanks.

MariaH
  • 331
  • 1
  • 8
  • 21

5 Answers5

4

You can simply call the String.replaceAll method and specify that those characters must be replaced by the empty String:

clean = clean.replaceAll("(?:--|[\\[\\]{}()+/\\\\])", "");

But if you need to do this many times, it's worth creating a Pattern object so that the regex does not have to be compiled repeatedly:

private static final Pattern UNWANTED_SYMBOLS =
        Pattern.compile("(?:--|[\\[\\]{}()+/\\\\])");

Now you can use this to create a Matcher object and use that to do the replacement:

Matcher unwantedMatcher = UNWANTED_SYMBOLS.matcher(clean);
clean = unwantedMatcher.replaceAll("");

This should be more efficient if you need to use the replacement in a loop which runs more than a few times.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
  • Is there a way to tell this pattern instead of replace `-` by an empty string, only remove it if there is two of them together `--` ? – MariaH Jun 24 '14 at 20:27
  • 1
    I've edited the regex pattern so that it should match a double-hyphen and not a single hyphen. – Bobulous Jun 24 '14 at 21:02
1

One, you do not escape using /, you do it using \.

Two, if you need to use \, you have to double escape it to get it into the regular expression.

Three, you can combine all the expressions into one regex.

Four, you can chain calls to replaceAll().

public class Replace { 
    public static void main(String[] args) {
        String clean = "This \\ is / an example. This -- is + an [(example)].";

        clean = clean.replaceAll("[\\[()\\]{}+\\\\\\/-]", "").replaceAll(" +", " ");
        System.out.println(clean.trim());
    }
}

Output:

This is an example. This is an example.
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

You should be able to remove everything in one fell swoop. Just put everything in a character class ([]).

[\[\]+{}()\\/-]

As in:

clean = clean.replaceAll("[\\[\\]+{}()\\\\/-]", "");
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
  • The Java regex engine doesn't seem to like `[][...]` - although the empty character group is invalid so the close bracket is ignored - the open bracket makes it angry... – Boris the Spider Jun 24 '14 at 20:17
  • @BoristheSpider Thanks, I guess I'll have to be on the safe side... – Kendall Frey Jun 24 '14 at 20:18
  • Ok. Just some particularities. - should be removed only if there are two together. And / should be replaced by a whitespace. I'm going to try to adapt your solutions here. Thanks. – MariaH Jun 24 '14 at 20:19
1

You may try:

String clean = "This \\ is / an example. This -- is + an [(example)].";

return clean.replaceAll("[(--)+\\[\\]{}()\\\\/]", "").trim());
M A
  • 71,713
  • 13
  • 134
  • 174
0

Replace / by \\ when escaping.

String clean = "This \\ is / an example. This -- is + an [(example)]."; // Had to change \ to \\

clean = clean.replaceAll("[\\[()\\]]", "");
clean = clean.replaceAll("\\-\\-", "");

clean = clean.replaceAll("\\/","");
clean = clean.replaceAll("\\\\", " ");
clean = clean.replaceAll("\\+", "");
kjaquier
  • 824
  • 4
  • 11