• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to replace "\\" character.

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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\>
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks but I don't have any luck.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic