Whats the difference between \z and \Z in a regular expression and when and how do I use it?

前端 未结 6 2000
鱼传尺愫
鱼传尺愫 2020-12-01 07:27

From http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html:

\\Z  The end of the input but for the fin         


        
6条回答
  •  我在风中等你
    2020-12-01 07:55

    Just checked it. It looks like when Matcher.matches() is invoked(like in your code, behind the scenes), \Z behaves like \z. However, when Matcher.find() is invoked, they behave differently as expected. The following returns true:

    Pattern p = Pattern.compile("StackOverflow\\Z");
    Matcher m = p.matcher("StackOverflow\n");
    System.out.println(m.find());
    

    and if you replace \Z with \z it returns false.

    I find this a little surprising...

提交回复
热议问题