-1

I have a string,

String string = "sdeLING9497896,\"kifssd9497777 999_13\",Kfsheis9497896e,ersdG9497896,aseLING9497896   erunk15426 \nEsea4521 
\"\nSdfes451 45264\" \"kiseliog949775 959_13\"";

I may have spaces, comma, tab or new line character in this string. My requirement is to split this string using space,comma,tab and new line but the spaces inside the double quoted strings should be excluded.

My sample code:

public class RegExTest {

    public static void main(String[] args) {
        Set<String> idSet = new HashSet<String>();
        String string = "sdeLING9497896,\"kipliog9497777 999_13\",KIPLING9497896e,ersdG9497896,aseLING9497896   erunk15426 \nEsea4521 \"\nSdfes451 45264\"";
        String ids[] = string.split(**"NEED A REGEX HERE"**);
        for (String id : ids) {
            if (id.trim().length() > 0) {
                idSet.add(id);
            }
        }
        System.out.println(idSet);
    }
}

My expectation is: sdeLING9497896, kifssd9497777 999_13, KIPLING9497896e, ersdG9497896, aseLING9497896, erunk15426, Esea4521, Sdfes451 45264, kiseliog949775 959_13

Please guide me to solve this!

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
Sasikumar
  • 111
  • 2
  • 8

1 Answers1

0

You can use this regex:

String[] tok = string.split("(?=((\\S*\\s){2})*\\S*$)[\\s,]+");

This will split on \s or , only if \s is outside double quotes by making sure there are even number of quotes after delimiter \s.

anubhava
  • 761,203
  • 64
  • 569
  • 643