In my app, I'd like to remove all text in between "example" and the first occurance after this of } from a string. And I want to do this for all occurences. So I use this code:
myString.replaceAll("\"example\"(.+?)}", "");
However, this gives me a PatternSyntaxException. Why? And: how do I solve it?
stack trace:
05-10 23:32:16.129: W/System.err(724): java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 16:
05-10 23:32:16.129: W/System.err(724): "example"(.+?)}
05-10 23:32:16.129: W/System.err(724): ^
05-10 23:32:16.159: W/System.err(724): at java.util.regex.Pattern.compileImpl(Native Method)
05-10 23:32:16.190: W/System.err(724): at java.util.regex.Pattern.compile(Pattern.java:400)
05-10 23:32:16.190: W/System.err(724): at java.util.regex.Pattern.<init>(Pattern.java:383)
05-10 23:32:16.219: W/System.err(724): at java.util.regex.Pattern.compile(Pattern.java:374)
05-10 23:32:16.219: W/System.err(724): at java.lang.String.replaceAll(String.java:1784)
...
System.out.println("\"example\" hello world } hello world".replaceAll("\"example\"(.+?)}", ""));and it worked fine for me. Do you have a stack trace to show?^is supposed to point to the character that's causing thePatternSyntaxException. That's what makes this exception so strange -- the character in question appears to be pointing at nothing in particular.