有人能告诉我为什么吗?
System.out.println("test".replaceAll(".*", "a"));结果:
aa请注意,下面的结果是相同的:
System.out.println("test".replaceAll(".*$", "a"));我已经在java6和7上测试过了,看起来两者的表现是一样的。我是不是遗漏了什么,或者这是java regex引擎中的一个bug?
发布于 2011-12-22 21:10:26
这不是异常:.*可以匹配任何内容。
您请求替换所有匹配项:
.*也匹配空字符串!因此,它匹配输入末尾的空字符串,并将其替换为a.使用.+不会出现这个问题,因为这个正则表达式不能匹配空字符串(它需要至少一个字符才能匹配)。
或者,使用.replaceFirst()仅替换第一个匹配项:
"test".replaceFirst(".*", "a")
^^^^^^^^^^^^现在,为什么.*的行为是这样的,并且匹配不超过两次(理论上是可以的),这是一个值得考虑的有趣的事情。如下所示:
# Before first run
regex: |.*
input: |whatever
# After first run
regex: .*|
input: whatever|
#before second run
regex: |.*
input: whatever|
#after second run: since .* can match an empty string, it it satisfied...
regex: .*|
input: whatever|
# However, this means the regex engine matched an empty input.
# All regex engines, in this situation, will shift
# one character further in the input.
# So, before third run, the situation is:
regex: |.*
input: whatever<|ExhaustionOfInput>
# Nothing can ever match here: out注意,正如@A.H.在注释中指出的那样,并不是所有的正则表达式引擎都是这样的。例如,GNU sed会认为它在第一次匹配后耗尽了输入。
https://stackoverflow.com/questions/8604286
复制相似问题