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

前端 未结 6 2006
鱼传尺愫
鱼传尺愫 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:57

    Like Eyal said, it works for find() but not for matches().

    This actually makes sense. The \Z anchor itself actually does match the position right before the final eol terminator, but the regular expression as a whole does not match, because, as a whole, it needs to match the entire text being matched, and nothing matches the terminator. (The \Z matches the position right before the terminator, which is not the same thing.)

    If you did "StackOverflow\n".matches("StackOverflow\\Z.*") you should be ok.

提交回复
热议问题