Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

java - Android | Syntax Error in regexp pattern

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...