相关疑难解决方法(0)

String.replaceAll(regex)进行两次相同的替换

谁能告诉我为什么

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正则表达式引擎中的错误吗?

java regex

41
推荐指数
1
解决办法
1742
查看次数

什么是正则表达式中\ z和\ Z之间的区别以及何时以及如何使用它?

来自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.但实际上两者都是假的.哪里出错了?

java regex

36
推荐指数
2
解决办法
3万
查看次数

标签 统计

java ×2

regex ×2