0

look at the following code:

String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:[email protected])FCR&DOC:[email protected]:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWong(T1281954U005) and Status is INCMP  on 10-JUN-11 11.28.45.764386 PM -05:00";
        //comment = comment.replaceAll("\\)", "\\\\)");
        //comment = comment.replaceAll("\\(", "\\\\(");
          if(comment == null || comment.length() < 100)
          {
            System.out.println();  
          }
         String[] strArray =    comment.split(" ");
         for (int i = 0; i < strArray.length; i++) 
           { 
              if(strArray[i].length() > 100)
               {
                 int iter = strArray[i].length() / 100 ;
                 int count = 100 ;
                 int initCount = 0 ;
                 String strReplace = null;

                    for(int j =0 ; j< iter ; j++)
                    {
                      strReplace = strArray[i].substring(initCount ,count); 

                      String strToReplace =  strReplace + "\n" ;
                      comment = comment.replaceAll(strReplace,strToReplace);
                      //comment = comment.replaceAll("\\)", "\\\\)");
                      //comment = comment.replaceAll("\\(", "\\\\(");
                      //comment = comment.replaceAll("\\\\", "");
                      System.out.println(comment);
                      System.out.println(comment.contains("\n"));   
                      initCount = count; //+1 ; 
                      count = count +100 ;
                    } 

                }   

            }
    }


When I run I get the following exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')'
near index 4 HK$.3)Booking:[email protected])FCR&DOC:[email protected]:00852-
23021977Fax:00852-2

From my understanding I have to escape the parantheses'(',')', I tried to do this(look at the commented part in the code)there was nt any exception but the newline I am appending to the string doesn't seem to appear.

3
  • Didn't get you question properly. Commented Jul 30, 2012 at 5:39
  • 1
    Maybe you can express this as a unit test to show what you expect the result to be. Commented Jul 30, 2012 at 5:43
  • @Pramod Kumar The purpose of the code is it will split a string with whitespace as delimiter and if any of the string split array exceeds 100 chars,I insert a newline(\n) and replace this new string with the original.What I meant was when you remove the comments in the above code,I dont get the exception but the newline(\n) am appending doesnt appear when I print the string finally. Commented Jul 30, 2012 at 5:49

1 Answer 1

4

String.replaceAll usess regular expressions for the first argument, and characters such as ) have special meaning when interpreted as regular expressions.

Try String.replace instead. (It still replaces all occurrences of the given substring.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.