Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

Why String.replaceAll() in java requires 4 slashes "\\" in regex to actually replace ""?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to esacpe twice, once for Java, once for the regex.

Java code is

""

makes a regex string of

"" - two chars

but the regex needs an escape too so it turns into

 - one symbol

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...