I recently noticed that, String.replaceAll(regex,replacement) behaves very weirdly when it comes to the escape-character ""(slash)
For example consider there is a string with filepath - String text = "E:\dummypath"
and we want to replace the ""
with "/"
.
text.replace("","/")
gives the output "E:/dummypath"
whereas text.replaceAll("","/")
raises the exception java.util.regex.PatternSyntaxException
.
If we want to implement the same functionality with replaceAll()
we need to write it as,
text.replaceAll("","/")
One notable difference is replaceAll()
has its arguments as reg-ex whereas replace()
has arguments character-sequence!
But text.replaceAll("
","/")
works exactly the same as its char-sequence equivalent text.replace("
","/")
Digging Deeper:
Even more weird behaviors can be observed when we try some other inputs.
Lets assign text="Hello
World
"
Now,
text.replaceAll("
","/")
, text.replaceAll("\n","/")
, text.replaceAll("\
","/")
all these three gives the same output Hello/World/
Java had really messed up with the reg-ex in its best possible way I feel! No other language seems to have these playful behaviors in reg-ex. Any specific reason, why Java messed up like this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…