3

I am using \\.(.*)} regex pattern to search a specific string in my Android Test Project. when i am using this regex to check on online available tools the regex looks fine. but in Android Test Project I am getting this following error.

java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 7:
\.(.*)}
^
at java.util.regex.Pattern.compileImpl(Native Method)
at java.util.regex.Pattern.compile(Pattern.java:400)
at java.util.regex.Pattern.<init>(Pattern.java:383)
at java.util.regex.Pattern.compile(Pattern.java:367)

What is the problem?

g.revolution
  • 11,962
  • 23
  • 81
  • 107

3 Answers3

11

You can try escaping your }: -

"\\.(.*)\\}"  // escaping `}` not needed in Java

I have no idea why it doesn't work in android, but in Java it works fine without escaping it.

However, if you are using an opening curly braces, then even in Java you would need to escape it: -

"\\.(.*)\\{"  // escaping `{` needed even in Java
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
8

You need to escape } as @Rohit Jain said:

String regex = "\\.(.*)\\}";

Your regex does work in java, but it fails on android for some reason.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
  • yups .. got it worked .. i escaped } also and it worked .. i did it before reading the answer though :D but i'll still accept this in 8 minutes :-p – g.revolution Nov 22 '12 at 09:10
  • i saw yours first.. and i refreshed also as you mentioned @rohit .. but rohit's answer was not not being shown to me .. it is now .. i have accepted his.. thanks for the answer – g.revolution Nov 22 '12 at 09:23
0

I was facing the same problem. I just surround it with a

 try{

 }catch (PatternSyntaxException e) {
           e.printStackTrace();
        }

and it worked.