0

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)
...
13
  • Might you have to escape the brace? Commented May 10, 2014 at 21:40
  • Could be. How do you do that? Commented May 10, 2014 at 21:42
  • 1
    Well, never mind... I just tried System.out.println("\"example\" hello world } hello world".replaceAll("\"example\"(.+?)}", "")); and it worked fine for me. Do you have a stack trace to show? Commented May 10, 2014 at 21:43
  • 1
    Hmmmm, how strange. What happens if you put a double backslash (`\` x2) before the last curly brace? (edit: turns out I needed to escape the backslashes here!) Commented May 10, 2014 at 22:07
  • 1
    The ^ is supposed to point to the character that's causing the PatternSyntaxException. That's what makes this exception so strange -- the character in question appears to be pointing at nothing in particular. Commented May 10, 2014 at 22:08

2 Answers 2

2

OK, so I don't understand why this gave an exception, but it seems that what you need to do is escape the last curly brace. So instead of

myString.replaceAll("\"example\"(.+?)}", "");

you do

myString.replaceAll("\"example\"(.+?)\\}", "");
                                     ^^

The first string worked for me in Java 1.7.0_51 and 1.8.0_05, so I'm not sure how this came about... But it works?

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

Comments

0
"\\\"example\\\"(.+?)}"

should work. Your string will be converted to a pattern, which means it will process the escape chars twice. so you need to type \\ for \ and \" for " .

Here is the example http://www.myregextester.com/?r=ab9d1f06

3 Comments

The thing is the example as provided worked just fine for me, and the issue seems to be that the regex compiler appears to be reading past the end of the regex string...
And I think the escape chars are actually for the Java compiler, not for the pattern compiler
This solution didn't help, but thank you for your help anyway!

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.