1

Please have a look at the following code.

String [] array=prettyHtml.split(" ");
String finalTxt="";

for(int i=0;i<array.length;i++)
{
     if(previousDiff.contains(array[i]))
     {
          finalTxt=prettyHtml.replaceAll(array[i], "");
      }
}

When executing the line finalTxt=prettyHtml.replaceAll(array[i], ""); I sometimes get the below error message, if the text had a + sign in it. Important to note, I am not always getting it anyway.

03-29 11:58:34.512: E/AndroidRuntime(27319): FATAL EXCEPTION: main
03-29 11:58:34.512: E/AndroidRuntime(27319): Process: com.phonegap.helloworld, PID: 27319
03-29 11:58:34.512: E/AndroidRuntime(27319): java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
03-29 11:58:34.512: E/AndroidRuntime(27319): +
03-29 11:58:34.512: E/AndroidRuntime(27319):  ^
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.util.regex.Pattern.compileImpl(Native Method)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.util.regex.Pattern.compile(Pattern.java:411)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.util.regex.Pattern.<init>(Pattern.java:394)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.util.regex.Pattern.compile(Pattern.java:381)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.lang.String.replaceAll(String.java:1785)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at SpeechRecPlug.receiveWhatWasHeard(SpeechRecPlug.java:436)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at SpeechRecPlug.access$1100(SpeechRecPlug.java:35)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at SpeechRecPlug$RecognitionListenerClass.onPartialResults(SpeechRecPlug.java:323)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at android.speech.SpeechRecognizer$InternalListener$1.handleMessage(SpeechRecognizer.java:451)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at android.os.Looper.loop(Looper.java:136)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at android.app.ActivityThread.main(ActivityThread.java:5426)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.lang.reflect.Method.invokeNative(Native Method)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at java.lang.reflect.Method.invoke(Method.java:515)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
03-29 11:58:34.512: E/AndroidRuntime(27319):    at dalvik.system.NativeStart.main(Native Method)

How can I solve this issue?

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • `finalTxt=prettyHtml.replace(array[i], "");` if your `array[i]` does not contain a regex pattern – Wiktor Stribiżew Mar 29 '16 at 06:38
  • @WiktorStribiżew: errr...sorry? I mean, OK, so how to make it to not to throw exceptions? In `split` I have seen `Pattern.quote` anyway – PeakGen Mar 29 '16 at 06:39
  • Or you may escape your regex String (since the String is considered a regular expression in `replaceAll`) : `Pattern.quote(array[i])` – Arnaud Mar 29 '16 at 06:40
  • @Berger: Cool. What's the output of it? Any difference? – PeakGen Mar 29 '16 at 06:41
  • @PeakGen : just use the above expression rather than the plain String. The `+` character has special meaning in a regex, as other ones, so this expression will make necessary escaping on your String . http://stackoverflow.com/questions/60160/how-to-escape-text-for-regular-expression-in-java – Arnaud Mar 29 '16 at 06:42
  • @Berger: so according to your comment, it should be `prettyHtml.replace(Pattern.quote(array[i]), "");` ? – PeakGen Mar 29 '16 at 06:43
  • @PeakGen : Yes exactly . – Arnaud Mar 29 '16 at 06:44
  • @Berger: Thanks. Will give a try. – PeakGen Mar 29 '16 at 06:44
  • @Berger: See [`String#replace()`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.replace%28java.lang.CharSequence%2Cjava.lang.CharSequence%29) reference - `replace` does the quoting and multiple replacements. It is Java, not JavaScript. – Wiktor Stribiżew Mar 29 '16 at 06:44
  • @WiktorStribiżew: So basically what you mean is, `string.replace()` will replace all the occurances of the given string? if I want to replace all occurance of `pop` word in a string, it will work? As you can see, my string will contain anything, numbers, words, + mark etc, I don't know exactly what the input is – PeakGen Mar 29 '16 at 06:52
  • I already posted what you should use in the first comment. There are tons of this same questions on SO, just search for "replaceAll does not work" in Google. Almost every SO solution says to use `replace` because it is made for literal string replacement - unless you want to use `replaceAll` with a compiled pattern - then use `Pattern.quote(search_string)` to use a literal search and replace. And that is also a common solution. – Wiktor Stribiżew Mar 29 '16 at 06:56

0 Answers0