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)
...
Xander
  • 5,487
  • 14
  • 49
  • 77
  • Might you have to escape the brace? – awksp May 10 '14 at 21:40
  • Could be. How do you do that? – Xander May 10 '14 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? – awksp May 10 '14 at 21:43
  • @user3580294 Updated the answer with a part of the stack trace – Xander May 10 '14 at 21:47
  • Hmmm, that's really strange... Perhaps there are some invisible non-ASCII characters that don't show up in the IDE but are in the source file? I've had problems with those in Eclipse before. Try deleting the line and rewriting from scratch. It's really strange that the Pattern compiler would keep looking beyond the end of the regex string.... – awksp May 10 '14 at 21:48
  • Rewritten it from the scratch. Still same problem, and same stack trace. How weird! – Xander May 10 '14 at 22:04
  • I don't understand why there's the ` ^` in the log file. Any ideas? – Xander May 10 '14 at 22:06
  • 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!) – awksp May 10 '14 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. – awksp May 10 '14 at 22:08
  • Can you post real code example which we could use to reproduce this problem? – Pshemo May 10 '14 at 22:21
  • @Merlin Also, what version of Java are you running? Your code works just fine for me on 1.8.0_05 and 1.7.0_51 – awksp May 10 '14 at 22:22
  • Yes! Escaping it did the trick! thank you so much for your patience and help! It's really appreciated. Make this an answer so I can accept it please – Xander May 10 '14 at 22:26
  • No problem! Will do. Glad I could help! Wish I told you how to do the escape first so it didn't have to take so long. – awksp May 10 '14 at 22:30

2 Answers2

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?

awksp
  • 11,764
  • 4
  • 37
  • 44
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

Onur
  • 5,617
  • 3
  • 26
  • 35
  • 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... – awksp May 10 '14 at 21:51
  • And I think the escape chars are actually for the Java compiler, not for the pattern compiler – awksp May 10 '14 at 21:52
  • This solution didn't help, but thank you for your help anyway! – Xander May 10 '14 at 22:04