File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need help in stringObj.replaceAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in stringObj.replaceAll()" Watch "Need help in stringObj.replaceAll()" New topic

Need help in stringObj.replaceAll()

Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
This line of code giving me errors.

I just want to replace single backslash with double. How to do that in a simple way. I have wrote a custom method to do this temporarily.

Please help me out folks.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Backslash has a special meaning in Java source code, so you have to escape it by doubling it. But backspace also has a special meaning in regular expressions, so you have to double it again. Try this:

"^(19|20)\\d{2}$".replaceAll("\\\\", "\\\\\\\\");

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
If you're using JDK 5, you can use

See also a similar discussion here.

"I'm not back." - Bill Harding, Twister
Babar Qadri
Ranch Hand

Joined: Oct 12, 2004
Posts: 51
Thanks Jesper, its working.
Thanks Jim for a valuable information.
I agree. Here's the link:
subject: Need help in stringObj.replaceAll()
It's not a secret anymore!