Results 1 to 4 of 4
Like Tree3Likes
  • 2 Post By 2by4
  • 1 Post By Fubarable

Thread: Problem with replace word

  1. #1
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Problem with replace word

    I am using replace word function,try to replace "\" to "/".
    this is my code:
    Java Code:
    import java.lang.*;
    public class test {
    
        public static void main(String[] args) {
            String str="We want \\ replace word from \\ this string";
    
            str=str.replaceAll("\\", "/");
    
            System.out.println(str);
        }
    }
    And it give me this error...
    Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected in
    ternal error near index 1
    \
    ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
    at test.main(test.java:7)
    I wonder why?
    Can anyone help me?

  2. #2
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with replace word

    Your first argument is a regular expression.

    Look at what needs to be passed in for a backslash..

    Java Code:
    Summary of regular-expression constructs
    Construct 	Matches
     
    Characters
    x 	The character x
    \\ 	The backslash character
    \0n 	The character with octal value 0n (0 <= n <= 7)
    \0nn 	The character with octal value 0nn (0 <= n <= 7)
    \0mnn 	The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
    ...
    source: Pattern (Java 2 Platform SE v1.4.2)

    Then figure out how to write that as a string literal.

    Hint: double escaping required.
    Fubarable and nananya like this.

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

    Default Re: Problem with replace word

    Or how about simply using replace(...) rather than replaceAll(...) since the former doesn't use regex, and regex isn't really needed here for this simple replace?
    nananya likes this.

  4. #4
    nananya is offline Member
    Join Date
    Dec 2011
    Posts
    20
    Rep Power
    0

    Default Re: Problem with replace word

    Thank you both,after i trying using replace(),it works.
    Thank you very much!

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2011, 05:44 PM
  2. String.replace ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 04:04 AM
  3. Word problem...PROBLEM!
    By aceoraiz in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-17-2010, 04:38 AM
  4. how to replace word in netbeans 6.9.1
    By enggvijaysingh@gmail.com in forum New To Java
    Replies: 1
    Last Post: 11-23-2010, 04:32 PM
  5. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 02:21 AM

Posting Permissions

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