Results 1 to 10 of 10
  1. #1
    nishiz is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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...

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    > 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. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    > 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. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

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

    Nice regex's...

  7. #7
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

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

    db

  9. #9
    radhabakshi is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    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. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Please see the date. I dont think the OP is still waiting for the answer in this thread

Similar Threads

  1. Help Replacing String
    By 7oclock in forum New To Java
    Replies: 5
    Last Post: 02-14-2009, 07:31 AM
  2. Object to String.
    By Gelembjuk in forum New To Java
    Replies: 6
    Last Post: 10-29-2008, 10:19 PM
  3. Replies: 1
    Last Post: 05-07-2008, 01:19 AM
  4. Object from String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:20 PM
  5. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM

Posting Permissions

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