gladiator
gladiator

Reputation: 1363

string split using regex and get data on both side

I am trying to parse a string as below but not able to get expected output In value pair there can be {} or [] or () and they may contain \n and = both

Please check key 4

3=dkjashd\n4={AAAAAA \n45=45\n AAAAAA \n AAAAAA}\n5=112

here output has to be

3=dkjashd

4={AAAAAA \n45=45\n AAAAAA \n AAAAAA}

5=112

As above key gives wrong result with "\h*\R\s*(?=\d*=)" and \s+(?=\d{1,2}=) both

        String req="1=A  \n2=B\n3={AAAAAA}\n4={AAAAAA \n45=45\n AAAAAA  \n AAAAAA} \n5=AAAAAA \n6=[10]\n7=[]\n8=[10,11]\n99=0";

        String []str=req.split("\\d{1,2}=");
        Arrays.stream(str).forEach(data-> System.out.println("Data :"+ data));

Result:

Data : Data :A

Data :B

Data :{AAAAAA}

Data :{AAAAAA
Data :45=45 AAAAAA AAAAAA}

Data :AAAAAA

Data :[10]

Data :[]

Data :[10,11]

Data :0

Expected :

Data :1=A

Data :2=B

Data :3={AAAAAA}

Data :4={AAAAAA 45=45 AAAAAA AAAAAA}

Data :5=AAAAAA

Data :6=[10]

Data :7=[]

Data :8=[10,11]

Data :99=0

Can anyone help over it.

Upvotes: 3

Views: 90

Answers (2)

anubhava
anubhava

Reputation: 785316

You can use this regex with a looahead:

String [] strarr = req.split(
   "\\h*\\n+\\h*(?=\\d{1,2}=(?![^{}]*})(?![^()]*\\))(?![^\\]\\[]*]))");
Arrays.stream(strarr).forEach(data-> System.out.println("Data : "+ data));
  • Regex \\h*\\n+\\h*(?=\\d{1,2}=(?![^{}]*})) splits on 1+ new lines surrounded by 0+ horizontal whitespaces that must be followed by 1 or 2 digits and = symbol.
  • Negative lookahead-1 (?![^{}]*}) asserts that we don't look for \d{1,2}= inside {...} .
  • Negative lookahead-2 (?![^()]*\)) asserts that we don't look for \d{1,2}= inside (...) .
  • Negative lookahead-3 (?![^\[\]]*]) asserts that we don't look for \d{1,2}= inside [...] .

(this is assuming you don't have escaped brackets)

You may also use:

Arrays.stream(strarr).forEach(data-> System.out.println("Data : "+data.replace("\n", "")));

if you don't want newline in output.

Output:

Data : 1=A
Data : 2=B
Data : 3={AAAAAA}
Data : 4={AAAAAA  AAAAAA   AAAAAA}
Data : 5=AAAAAA
Data : 6=[10]
Data : 7=[]
Data : 8=[10,11]
Data : 99=0

Upvotes: 7

Nir Alfasi
Nir Alfasi

Reputation: 53535

You can use pattern/matcher to extract the required parts:

String req="1=A  \n2=B\n3={AAAAAA}\n4={AAAAAA \n AAAAAA  \n AAAAAA} \n5=AAAAAA \n6=[10]\n7=[]\n8=[10,11]\n99=0";
Pattern pattern = Pattern.compile("(\\d+=.*?)\\s");
Matcher matcher = pattern.matcher(req);

while (matcher.find()) {
    System.out.println("Data :"+ matcher.group(1));
}

OUTPUT

Data :1=A
Data :2=B
Data :3={AAAAAA}
Data :4={AAAAAA
Data :5=AAAAAA
Data :6=[10]
Data :7=[]
Data :8=[10,11]

Upvotes: 2

Related Questions