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?

1
  • 1
    Can you post the string for which you are using it? Commented Nov 22, 2012 at 9:06

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

i needed to use both open and closed curly brackets and i had to put escapes for both..
@g.revolution.. Actually, didn't knew that it works that way in Android.
thanx for the answer though .. @Nikita mentioned your answer was first :D .. so selecting yours as correct one.
this answer explains why it does not work in Android.
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.

2 Comments

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
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
0

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

 try{

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

and it worked.

Comments

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.