# Thread: replacing backslashes in String Object

1. Member
Join Date
Feb 2009
Posts
1
Rep Power
0

## replacing backslashes in String Object

I have a peculiar usecase where i recieves a string as an input. A string may contain a single back slash or double. e.gs

"abc\def"
abc\def\\ghi"

what i need to do is that if a string contains a single back slash, i need to convert them to double. So the above strings should be converted to :

"abc\\def"
"abc\\def\\ghi"

So if there is a double backslash, it should not be hampered.

The replaceAll method of string takes regular expression to match and replace. The regular expression for back slash is [\\]. But it throws an exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Pattern.java:1686)
at java.util.regex.Pattern.compile(Pattern.java:1439)

2. Let's see a very small compilable program that demonstrates your problem. I'll bet that you're getting your regex for backslash wrong, but I need to see your code to know.

3. Try replaceAll("\\\\+", "/")

That's not what you asked for, but...

First of all, both Java and regex use backslash as an escape character. To get one backslash after escapes, you need four of them.

Second, finding one backslash, by itself, requires a look-ahead expression, which can be done, but it is more complicated.

This approach reduces all backslashes to a single forward slash. You could use some other character.

At this point, you have two choices:

Do a second replace and turn the forward slash into two backslashes, remembering to escape the replacement characters.

Leave the forward slash alone. The main use of backslashes is Windows directory paths, and Windows paths can use forward slashes just as well. And forward slashes are compatible with UNIX file systems and are generally just much easier.

I hope this makes sense...
Last edited by Steve11235; 03-01-2009 at 06:54 PM. Reason: Fix typos...

4. > This approach reduces all backslashes to a single forward slash.
Steve, what if the String can legally contain a forward slash, which is not to be replaced? One would have to write a loop to identify some character not contained in the String, and thos could be expensive.

> Second, finding one backslash, by itself, requires a look-ahead expression
Also a look-behind.

It wowld help if nishiz were to explain the reason behind this backslash doubling.

db

5. > The replaceAll method of string takes regular expression to match and replace. The regular expression for back slash is [\\]. But it throws an exception:

When you assign a String literal with
Java Code:
String regex = "[\\]";
the content of the String is
Java Code:
"[\]"
Since regex also uses the backslash as an escape character, the closing bracket is quoted and the character class is left unclosed.

It's often useful to work backwards. Here you need to match a single backslash: \

The pattern to match that would be \\

The String literal to create that pattern would have to have one backslash to escape each of the two backslashes: \\\\

The replacement String can also create a problem. Read the documentation for String.replaceAll:
quote:
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string unquote One way around this, which I find more readable, is to use a capturing group. Here are four expressions that do the job. The second and fourth use the group that captured a single backslash for the replacement. The first and third use a cumbersome 8 backslashes to obtain the two. Java Code: public class DoubleTheBackslash { public static void main(String[] args) { String[] inputs = {"abc\\\\def", "abc\\def\\\\ghi"}; showReplacements(inputs, "(?<!\\\\)\\\$$?!\\\$$", "\\\\\\\\"); showReplacements(inputs, "(?<!\\\\)\\\$$?!\\\$$", "$0$0"); showReplacements(inputs, "([^\\\\])(\\\\)([^\\\\])", "$1\\\\\\\\$3"); showReplacements(inputs, "([^\\\\])(\\\\)([^\\\\])", "$1$2$2\$3");
}

private static void showReplacements(String[] inputs,
String regex, String replaceWith) {
for (String input : inputs) {
System.out.println(input);
System.out.println(input.replaceAll(regex, replaceWith));
System.out.println();
}
}
}
db

6. Darryl, what you said is true. I left out some of the "fine print" to stay to the point.

Nice regex's...

7. String word = "abcd/efg/123";
String word2 = word.replaceAll("/", "//");

8. azzaiel, what does your response have to do with the question, which is about replacing backslashes, not forward slashes?

db

9. Member
Join Date
Dec 2010
Posts
1
Rep Power
0
This code will replace one backslash with 2.
String s = "asd\\sdf";
System.out.println(s + " becomes: ");
s = s.replaceAll("\\\\", "\\\\\\\\");
System.out.println(s);

10. Senior Member
Join Date
Apr 2010
Location
Philippines
Posts
580
Rep Power
4
Please see the date. I dont think the OP is still waiting for the answer in this thread

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•