aspose file tools*
The moose likes Java in General and the fly likes How to replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to replace "\\" character." Watch "How to replace "\\" character." New topic
Author

How to replace "\\" character.

John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Hello JGuru,
I would like to replace "\\" single back slash character but do not how. Any idea? Thanks.

John

import java.io.*;

public class Parser
{
public static void main(String[] args)
{
String fileName = args[0];

try
{
FileReaderfilereader = new FileReader(fileName);
BufferedReader bufferedreader = new BufferedReader(filereader);
Stringstring = bufferedreader.readLine();
System.out.println(string);

string = string.replaceAll("\\", "");
System.out.println(string);

}
catch (Exception e)
{}
}
}

input.txt
\<B\>\<C\>\<F28\>TRANSFER COVER SHEET \</F28\>\</C\>\</B\>
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Your code is basically correct.

Your replaceAll() statement doesn't reflect that the JVM will parse that string before it operates on it.

Just replace it with this line:
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Jeff, Thanks for your input. But I need replace "\<B\>" to be "<B>" and "\\" to "\". The solution will strip out all the slashes. I have in input file literal like this

\\\\remote_server\\some_dir
\<B\>bold character\</B\>

I would like to convert to
\\remote_server\some_dir
<B>bold character</B>

I am sorry to drill a bit deeper but the nature of this complexity drives me nut. Would you suggest another alternative? Thanks

Respectfully.

John
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Try



This replaces every backslash followed by a non-backslash-character by only the latter character. It will therefore also reduce any double-backslash that is not at the end of the line (and therefore followed by another character) to a single backslash. (This is from pure memory, though - I didn't test it.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John McDonald
Ranch Hand

Joined: Jul 01, 2003
Posts: 112
Thanks but I don't have any luck.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
These regex problems are tough. It's real easy to spin your wheels on this stuff.

I've had some success with your example data and the following code:


For me (see my quick comment there) I have to keep in mind that four slashes to the JVM actually mean 1 slash for the humans...

This code will replace the four slashes it sees (aka 16 slashes for the JVM) with two slashes (aka 8 slashes). Then it replaces all the single slashes it sees with nothing.

Hope this helps.
[ November 15, 2004: Message edited by: jefff willis ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
upon further inspection...that's not really working either.

This works, but it's hardcoded to your input. It's not a solution unless all you will ever have are \> and \<.




[ November 15, 2004: Message edited by: jefff willis ]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
There's no need to treat an escaped backslash differently than any other escaped character:Note: it's $1 in the replacement string, not \1.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace "\\" character.