1

I have a file with a long string a I would like to split it by specific item i.e.

String line = "{{[Metadata{"this, is my first, string"}]},{[Metadata{"this, is my second, string"}]},{[Metadata{"this, is my third string"}]}}"

String[] tab = line.split("(?=\\bMetadata\\b)");

So now when I iterate my tab I will get lines starting from word: "Metadata" but I would like lines starting from:

"{[Metadata"

I've tried something like:

 String[] tab = line.split("(?=\\b{[Metadata\\b)");

but it doesnt work. Can anyone help me how to do that, plese?

2
  • 1
    You may try "(?=\\{\\[Metadata\\b)". See ideone.com/LwB7Z2 Commented Dec 20, 2018 at 13:30
  • Yes, this one works :) thanks Commented Dec 20, 2018 at 13:47

2 Answers 2

2

You may use

(?=\{\[Metadata\b)

See a demo on regex101.com.


Note that the backslashes need to be escaped in Java so that it becomes

(?=\\{\\[Metadata\\b)
Sign up to request clarification or add additional context in comments.

Comments

0

Here is solution using a formal pattern matcher. We can try matching your content using the following regex:

(?<=Metadata\\{\")[^\"]+

This uses a lookbehind to check for the Metadata marker, ending with a double quote. Then, it matches any content up to the closing double quote.

String line = "{{[Metadata{\"this, is my first, string\"}]},{[Metadata{\"this, is my second, string\"}]},{[Metadata{\"this, is my third string\"}]}}";
String pattern = "(?<=Metadata\\{\")[^\"]+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);

while (m.find( )) {
    System.out.println(m.group(0));
}

this, is my first, string
this, is my second, string
this, is my third string

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.