谁能告诉我为什么
System.out.println("test".replaceAll(".*", "a"));
Run Code Online (Sandbox Code Playgroud)
结果是
aa
Run Code Online (Sandbox Code Playgroud)
请注意,以下结果相同:
System.out.println("test".replaceAll(".*$", "a"));
Run Code Online (Sandbox Code Playgroud)
我已经在java 6和7上测试了它,两者似乎都表现得一样.我错过了什么或者这是java正则表达式引擎中的错误吗?
来自http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html:
\Z The end of the input but for the final terminator, if any
\z The end of the input
Run Code Online (Sandbox Code Playgroud)
但这在实践中意味着什么?当我使用\ Z或\ z时,你能举个例子吗?
在我的测试中,我认为这"StackOverflow\n".matches("StackOverflow\\z")将返回true并"StackOverflow\n".matches("StackOverflow\\Z")返回false.但实际上两者都是假的.哪里出错了?