Results 1 to 10 of 10
- 02-28-2009, 12:29 PM #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)
Can anyone , please help.
-
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.
- 03-01-2009, 05:53 PM #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 05:54 PM. Reason: Fix typos...
- 03-01-2009, 08:35 PM #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
- 03-01-2009, 08:42 PM #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 withthe content of the String isJava Code:String regex = "[\\]";
Since regex also uses the backslash as an escape character, the closing bracket is quoted and the character class is left unclosed.Java Code:"[\]"
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.dbJava 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(); } } }
- 03-02-2009, 02:40 AM #6
Darryl, what you said is true. I left out some of the "fine print" to stay to the point.
Nice regex's...
- 03-02-2009, 02:50 AM #7
String word = "abcd/efg/123";
String word2 = word.replaceAll("/", "//");
- 03-02-2009, 08:45 AM #8
azzaiel, what does your response have to do with the question, which is about replacing backslashes, not forward slashes?
db
- 12-01-2010, 06:30 PM #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);
- 12-02-2010, 12:31 AM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Similar Threads
-
Help Replacing String
By 7oclock in forum New To JavaReplies: 5Last Post: 02-14-2009, 07:31 AM -
Object to String.
By Gelembjuk in forum New To JavaReplies: 6Last Post: 10-29-2008, 10:19 PM -
replacing last comma in a string with the word "or"
By wprjr in forum New To JavaReplies: 1Last Post: 05-07-2008, 01:19 AM -
Object from String
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:20 PM -
splitting string and replacing
By itsme in forum New To JavaReplies: 1Last Post: 12-11-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks