bindhukc
bindhukc

Reputation: 21

Replace \ and newline replace in java

I am writing code to detect bad keywords in a file. Here are the steps that I follow:

  1. Tokenize using StreamTokenizer
  2. Use pattern matcher to find the matches

    while(streamTokenizer.nextToken() != StreamTokenizer.TT_EOF){
                if(streamTokenizer.ttype == StreamTokenizer.TT_WORD) {
                    String token = streamTokenizer.sval.trim().replaceAll("\\\\n", "")
                    final Matcher matcher = badKeywordPattern.matcher(token)
                    if(matcher.find()) { // bad tokens found
                         return true;
                    }
                }
    }
    

String token = streamTokenizer.sval.trim().replaceAll("\\\\n", "") is done to match token spanning multiple lines with \. Example:

bad\ token

However the replace is not working. Any suggestions? Any other ways to do this?

Upvotes: 2

Views: 171

Answers (3)

Boann
Boann

Reputation: 50041

If I understand correctly, you do not want to use regex (which is what String.replaceAll does), just do literal string replacement with String.replace, and use one fewer backslash:

String token = streamTokenizer.sval.trim().replace("\\\n", "")

Upvotes: 1

Kh.Taheri
Kh.Taheri

Reputation: 957

Based on @Pshemo answer, which shows you how \ & \n presented in regex, and as mentioned here. You could do it like this:

String[] tkns = streamTokenizer.sval.trim().split("\\\\\\R"); // yourString = "bad\\\ntaken"
StringBuffer token= new StringBuffer();
for (String tkn : tkns)
{
    token.append(tkn);
    //System.out.println(tkn);
}
//final Matcher matcher = badKeywordPattern.matcher(token)

Upvotes: 0

Pshemo
Pshemo

Reputation: 124225

Assuming you want to remove all \ placed at end of the line, along with line separator you could use replaceAll("\\\\\\R","").

  • To represent \ in regex (which is what replaceAll uses) we need to escape it with another \, which leaves us with \\. But since \ is also special in String literals we need to escape each of them again with another backslash which leaves us with "\\\\"

  • Since Java 8 we can use \R (which needs to be written as "\\R" since \ requires escaping) to represent line separators like \r \n or \r\n pair.

Upvotes: 1

Related Questions