12 Replies Latest reply: Feb 24, 2007 12:06 PM by 796440 RSS

    Substring error with ( or  )

    807606
      Hi, i'm trying to do a substring in a string.. using:
      txt.replaceAll(":(", "SAD");
      but get an error
      "Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unclosed group near index 2
      :("
      or when i use:
      txt.replaceAll(":(", "HAPPY");
      I get the error:
      "Unmatched closing ')' near index 0 :)"
        • 1. Re: Substring error with ( or  )
          807606
          Not sure about this, but try using replaceAll(":/(", "SAD"); instead, I didn't think you would have to use the escape character here, but it's worth a try
          • 2. Re: Substring error with ( or  )
            807606
            thanks, tried it but still same error :(
            • 3. Re: Substring error with ( or  )
              807606
              txt.replaceAll(":\\(", "SAD");
              That should work. You needed to escape the parenthesis.
              • 4. Re: Substring error with ( or  )
                807606
                Awsome.. thanks it worked.. so basically i put a "\\" before any character such as (,),@, etc... yes.
                • 5. Re: Substring error with ( or  )
                  807606
                  Awsome.. thanks it worked.. so basically i put a "\\"
                  before any character such as (,),@, etc... yes.
                  No. You need to escape all the metacharacters used for regular expressions. They are:
                  ([{\^-$|]})?*+.
                  • 7. Re: Substring error with ( or  )
                    807606
                    thanks worked but got this other problem.. would do a new thread but will be taking up space so as its sought of related to this thread not sure if you can help... basically i've made it get a substring and then 2 consecutive replaceAll checks using :
                        public String getTxtMult(String str){
                             String txt = str;
                            int s = 0;
                            int e = 2;
                          
                            for (int i = 0; i < txt.length(); i++) {
                                 System.out.println("Comparing: " + txt.substring(s, e) + " with :)");
                                 if (txt.substring(s, e).equals(":)")) {
                                     txt =  txt.replaceAll(":\\)", "HAPPY");
                                 }
                                 System.out.println("Comparing: " + txt.substring(s, e) + " with :(");
                                 if (txt.substring(s, e).equals(":(")) {
                                     txt =  txt.replaceAll(":\\(", "SAD");
                                 }
                    
                                s = s+1;
                                e = e+1;
                    
                                 if (e == str.length()+1)  {
                                      break;
                                 }
                            }
                            System.out.println("Completed: " + txt);
                            return txt;
                            } 
                    Now what I noticed is lets say i put a string "Hi :) :(" it will put out: "Hi HAPPY :(" when it should put out "Hi HAPPY SAD" but wheni put anything after the last substring then it brings it up.. so if i put "Hi :) :( sds" it will work.. ?
                    • 8. Re: Substring error with ( or  )
                      807606
                      if (e == str.length()+1)
                      Try changing this to:
                      if (e == txt.length()+1)
                      • 9. Re: Substring error with ( or  )
                        807606
                        ahh for a second i thought thats it silly me!.. but nope still same thing :(..

                        a couple of things I noticed.. not sure if they will help

                        1] the 2nd substring/smily, needs to have atleast 3 characters wether it be blank spaces or letters etc after it for it to work.

                        2] its only the 2nd smiley (substring).. in the string that it has this problems with.. because if that works the rest automaticlaly work..

                        3] if its the same smiley then it works but if its 2 diff ones then it has the problem.

                        Message was edited by:
                        QTQ
                        • 10. Re: Substring error with ( or  )
                          796440
                          You don't even need to do the substring part--replaceAll already does that.
                          String str = "Hi :) :(" ;
                          str = str.replaceAll(":\\)", "HAPPY").replaceAll(":\\(", "SAD");
                          <Hi HAPPY SAD>
                          • 11. Re: Substring error with ( or  )
                            807606
                            actually hey thanks it worked!!! my mistake i changed the variable in the wrong method, well i had a backup method, adn i changed it in there, and it never called it.. hehe :) awsome thanks again! you've been a great help!
                            • 12. Re: Substring error with ( or  )
                              807606
                              jverd ... oh i didn't know that.. awsome will give that a go! thanks