user2050089 user2050089 - 1 year ago 73
Java Question

How to split to parse below string "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy"

I have a String,

String data = "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy";

I want to split it as below,


String[] array = data.split(?);

What regex can be used to get the exact output?

Answer Source

It's not pretty, and probably not the best solution, but it does what you want. So preferably I would like someone else to post a better answer or you'll have to wait for me to write one.

public class Test {
    public static void main(String[] args) {
        String data = "foo=ds,bar=sds,sdsd,qual=baz,blurb,junk=quux,syzygy";
        String[] array = data.split(",");
        for(String s : array) {
            if(s.contains("=")) {
                System.out.print("\n" + s);
            } else {
                System.out.print("," + s);

I know that this prints an empty line first and if you really want to remove this then you can add an if-statement for that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download