7

I have a java string that looks like this;

(fname:jon)(lname:doe)(guaranteer: Sam (W) Willis)(age:35)(addr:1 Turnpike Plaza)(favcolor:blue)

And I want to split this String from delimiter (fname:jon)<here>(lname:doe).

I tried splitting through regex \)\( but it just breaks my code

arr = s.split("\\)\\(");
for (String a: arr) System.out.println(a);

Output

(fname:jon
lname:doe
guaranteer: Sam (W) Willis
age:35
addr:1 Turnpike Plaza
favcolor:blue)

I also looked at this question: How to split a string, but also keep the delimiters?, but it didn't helped because in my case, I want to keep the delimiter )( and split delimiter evenly as well, i.e., the first bracket should go to first result and second to second result.

The regex that i used was s.split("(?<=\\)\\()") and it gave output:

(fname:jon)(
lname:doe)(
guaranteer: Sam (W) Willis)(
age:35)(
addr:1 Turnpike Plaza)(
favcolor:blue)

This is my desired output:

(fname:jon)
(lname:doe)
(guaranteer: Sam (W) Willis)
(age:35)
(addr:1 Turnpike Plaza)
(favcolor:blue)
Community
  • 1
  • 1
Cupid
  • 221
  • 1
  • 5

2 Answers2

8

You can split your string using positive lookahead and positive lookbehind like this:

RegEx (?<=\))(?=\()

DEMO

  1. (?<=\)) positive lookbehind which indicates that closing bracket should preceed the split position.
  2. (?=\() positive lookahead which indicates that opening bracket should follow that split position.

Output

(fname:jon)
(lname:doe)
(guaranteer: Sam (W) Willis)
(age:35)
(addr:1 Turnpike Plaza)
(favcolor:blue)

Code

String s = "(fname:jon)(lname:doe)(guaranteer: Sam (W) Willis)(age:35)(addr:1 Turnpike Plaza)(favcolor:blue)";
String arr[] = s.split("(?<=\\))(?=\\()");
for (String a: arr) System.out.println(a);
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • Delete the pointless `.{0}` - it has no effect whatever, And you are incorrect: There is no "null" character matched here, the null char is `'\0'`. The match is *between* characters based on the look arounds, which is nothing particularly special. – Bohemian Mar 04 '17 at 14:23
  • @Bohemian I originally kept `.{0}` just to indicate that *this is the delimiter*. By null I actually meant *the empty position between positive lookahead and positive lookbehind*. However, thanks for pointing it out, I've updated my answer accordingly. – Raman Sahasi Mar 04 '17 at 15:17
  • You haven't really update all your post, particularly the first sentence – Bohemian Mar 04 '17 at 15:41
  • @Bohemian updated. – Raman Sahasi Mar 04 '17 at 19:00
7

You can use a positive lookbehind and negative lookahead to achieve this :

public static void main(String[] args) throws Exception {
    String s = "(fname:jon)(lname:doe)(guaranteer: Sam (W) Willis)(age:35)"
            + "(addr:1 Turnpike Plaza)(favcolor:blue)";
    String[] arr = s.split("(?<=\\)(?!\\s))"); // positive lookbehind for ")" and negative lookahead for "space"
    for (String str : arr) {
        System.out.println(str);
    }

}

O/P :

(fname:jon)
(lname:doe)
(guaranteer: Sam (W) Willis)
(age:35)
(addr:1 Turnpike Plaza)
(favcolor:blue)
TheLostMind
  • 35,966
  • 12
  • 68
  • 104