开发者

Why isn't \\ interpreted as a blackslash in this regex?

I'm learning to use 开发者_StackOverflow中文版Java's Pattern and Matcher, and this is an example code snippet in my book. It works as the author describes, but what I don't get is why \\. ends up being a dot instead of a backslash (the \\ part) and a dot (the . part). Does the compiler not read from left to right?

import java.util.regex.*;
public class SplitTest {
   public static void main(String[] args)  {
       String input= "www.cs.cornell.edu";                          

      Pattern p = Pattern.compile("\\.");
      String pieces[] = p.split(input);
      for (int i=0; i<pieces.length; i++){
            System.out.println(pieces[i]);    
            }



   }
}


It's being interpreted once when parsing the string literal, and once by the regex compiler.

"\\." -> "\." - string literal
"\." -> literal . - regex compiler


You must double-escape the string literal. "\\\\." Because Java interprets the string literal "\\." as \., which is not what you expect. Try this: System.out.println("\\."), and what you see is what you get in the regexp.

EDIT: Your input string is "www.cs.cornell.edu". Do you know what you are doing? Maybe you are trying to split by the dot (\.), which its Java literal is "\\." as you typed.

Maybe you are trying to match a BACKSLASH then a DOT, which means its regex is \\\., which its Java literal is "\\\\\\."


Your code can be simplified a bit, like so:

public class SplitTest {
    public static void main(String[] args) {
        String input = "www.cs.cornell.edu";
        String[] pieces = input.split("\\.");
        for (String piece : pieces) {
            System.out.println(piece);
        }
    }
}
The "double back slash period" works just as expected in this case, but the formatting on stackoverflow requires "quadruple back slash period" which is kind of odd.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜