Home > Mobile >  Regex with special chars does not match "\\t" in input
Regex with special chars does not match "\\t" in input

Time:10-05

Any ideas why this prints "Not found"? How do I split on \\t (not tab, but the actual characters \ and t)? I don't get it. I'm even debugging and see the same character codes both in the input and in the separator (92 for \ and 116 for t).

    public static void main(String[] args) {
        String testString = "0.\\tnodeId=\\\"abcde\\\"\\n1.\\txyz_=\\\"help\\\"\\n2.";
        String separator  = "\\t";
        Matcher m = Pattern.compile(separator, Pattern.MULTILINE).matcher(testString);
        if (m.find()) {
            System.out.println("Found pattern!");
        } else {
            System.out.println("Not found!");
        }
    }

CodePudding user response:

The first argument of Pattern.compile(String, int) is a regex. Your string "\\t" contains the characters \t, so the regex will search for a literal tab character. You want the regex \\t, so your string has to be "\\\\t":

        String separatorRegex = "\\\\t";
        Matcher m = Pattern.compile(separatorRegex, Pattern.MULTILINE).matcher(testString);

With this, the code outputs “Found pattern!”: ideone.com/VrSaHD

CodePudding user response:

The Pattern.MULTILINE is wrong.

In multiline mode, the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default, these expressions only match at the beginning and the end of the entire input sequence.

Try using Pattern.LITERAL instead.

When this flag is specified then the input string that specifies the pattern is treated as a sequence of literal characters. Metacharacters or escape sequences in the input sequence will be given no special meaning.

If you do that, your code will work without needing any other changes: ideone.com/ZNsB1T

Read more about the Pattern class here.

  • Related