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
262 views
in Technique[技术] by (71.8m points)

java - PatternSyntaxException while trying to split by },{

I am trying to break up an array I got through an API on a site, which Java has retrieved as a String.

String[] ex = exampleString.split("},{");

A PatternSyntaxException is thrown. For some reason, it really doesn't like },{. I have tried escaping it as {, but it says it is an illegal escape.

What is the proper way to escape this string?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

For some reason, it really doesn't like },{.

This is because braces (} and {) are special characters in Java regular expressions. If you try to use them literally without escaping, it's considered a syntax error, hence your exception.

What is the proper way to escape this String?

Escape the backslashes too, by doubling them. This is for Java string escapes. The escaped backslashes will then escape the braces for the regex.

String[] ex = exampleString.split("\},\{");

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

...