1

I have a String in Java. Here is the part of it that I'm concerned with {3: {108:TR2011052300088}}

Later on I do a split on {3: {108:. For some reason (I've been googling) { and } is a special character so it has to be escaped \} and \{ (clearly this doesn't work -> compile time error).

Others mention this is some bug in Java regex. I'm not sure really. The exception I get is:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed counted closure near index 2 {3:{108: at java.util.regex.Pattern.error(Unknown Source)

Long story short, my code splits the string using {3: {108: as the separator and crashes on it:

String query="{3: {108:";
String [] messageParts = message.split(query);

I am aware of other ways to do it, albeit more complicated, like writing my own parser and such.

How can I do my string split and not have it crash?

EDIT: To answer some comments:
- Double slashes don't help: \\{ give \{3:\{108:MAMABEARid123}} since 2 slashes become 1
- Escaping with 1 slash won't compile: Invalid Escape sequence

7
  • Perhaps escaping the } would help? It's telling you that it's trying to interpret the brackets. Commented Mar 7, 2012 at 16:06
  • yea, but how do I escape them? \{ doesnt work. Commented Mar 7, 2012 at 16:08
  • When you say "clearly this doesn't work" it would help if you'd say in what way it didn't work. Commented Mar 7, 2012 at 16:08
  • 3
    to escape use \\} and \\{ Commented Mar 7, 2012 at 16:08
  • @Jon Skeet I get compile time error if I used "\{" Commented Mar 7, 2012 at 16:09

4 Answers 4

6

Example to escape the {:

    public static void main(String[] args) {
        String s = "{3: {108:TR2011052300088}}";
        String[] ss  = s.split("\\{3: \\{108:");
        System.out.println(ss[1]); //prints TR2011052300088}}
    }
Sign up to request clarification or add additional context in comments.

1 Comment

it turns out I had to do a clean build of my project in order to be able to add \\{ to a String. Just FYI I combine the pattern with the result of the split so that's escaped differently by when I print it ugh
0

Assuming you don't really need regular expressions, I'd use Guava's Splitter class.

private static final Splitter SPLITTER = Splitter.on("{3: {108:");

...

Iterable<String> bits = SPLITTER.split(messageParts);

(You can use Iterables.toArray to create an array from that, or Lists.newArrayList for a list, or whatever...)

Comments

0

Remove the curly brackets and then split on colon:

String inp = "{3: {108:TR2011052300088}}";

System.out.println(inp.replaceAll("[\\{\\}]", "").split(":")[2])

Prints 'TR2011052300088'

Comments

-2

Use [{] ..... I've worked with Regex for Swift messages before

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.