0

I'm trying to parse through a string to get rid of garbage characters, in this example it's things like: &,%,$,#,*,@,etc. But I want to keep spaces and parentheses, right now I'm using a delimiter on a string and just building the string off of that:

public TokenIter(String line){
    line.trim();
    Scanner scr = new Scanner(line);

    String built = "";
    scr.useDelimiter("[^A-Za-Z]+|^)|^(|^ ");
    while (scr.hasNext()){
        built+= scr.next();
    }
this.line = built.trim();

I also tried using a regex and .matches() but for some reason that screwed up the scanner. Any help on how to set up the delimiter would be appreciated!

  • you can build a regex like this: `\\&|\\%|\\%|\\$|\\#|\\*|\\@` for every character you don't want in your string and then do `built+= scr.next().replaceAll("regex","");` – XtremeBaumer Feb 24 '17 at 07:26

1 Answers1

1

What you could do is to use string.replaceAll which accepts a regex, and replaces all occurrences of it with a given string.

public void TokenIter(String line) {
    this.line = line.trim().replaceAll("[&%$#*@]+", "");
}
Hesham Attia
  • 979
  • 8
  • 13
  • Unless they're escaped for Java's sake, none of those characters should need escaped. Within `[ ]`, the only things that generally need escaped are `]` and backslash itself. – Regular Jo Feb 24 '17 at 07:52
  • 1
    Updating for anyone that might stumble on this answer, hyphen too. Hyphens should always be at the start or end or character class or escaped, otherwise they'll wreak unpredictable havoc. Shame OP never came back to say thanks, the +1 is mine. – Regular Jo Feb 24 '17 at 22:00