0

I have the following data text :

[data]A[/data]
aaa
4 5 8 
[data]B[/data]
bbb
3 1 9 
[data]C[/data]
ccc
6 5 2
... 

I want to split them in to the following 3 units :

1st unit :

[data]A[/data]
aaa
4 5 8 

2nd unit :

[data]B[/data]
bbb
3 1 9 

3rd unit :

[data]C[/data]
ccc
6 5 2

So my code looks like this :

String Units[]=dataText.split("[data]");

Yet, this doesn't do it properly, what's the right way to split it ?

If I use regex, how should I write the expression ?

1

2 Answers 2

3

Use regex (?ms)(?<=.)(?=^\[):

    String[] units = dataText.split("(?ms)(?<=.)(?=^\\[)");

See regex101.com for demo.

Explanation:

(?ms)       Turn on MULTILINE ('^' and '$' match after/before line terminator)
                and DOTALL (aka "single line", '.' matches any character)
(?<=.)      Must be preceded by a character (needs 's' flag)
               Used to prevent matching very first '['
(?=^\[)     Must be followed by a '[' at the beginning of a line (needs 'm' flag)
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a BufferedReader:

BufferedReader br = new BufferedReader(new StringReader(dataString));

Iterate the String like this:

int lineCounter = 0;
int arrayCounter = 0;
String line = null;
while( (line = br.readLine()) != null )
{
    units[arrayCounter] += line;
    if (lineCounter >= 2) {
        arrayCounter++;
        lineCounter = 0;
    }
}

5 Comments

OK, but how to decide the size of units ? My dataString could include hounds of units.
Your example looked like 3 lines per unit. If this size is not constant, you will have to use a regex.
Yes, it is a clean solution.
What I meant was, each unit has 3 lines, that's fixed, but there could be 216 units or 567 units, so how to create "String units=new String[ ??? ]"
You have to use a collection like ArrayList.

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.