Question: ```
String filter = phoneNumber eq '763436' and  carrier eq 'AT and T Mobility' and site startswith '256'

```

the split should give me the following:

> 
> String 1 = phoneNumber eq 763436
> 
> 
> String 2 = carrier eq 'AT and T Mobility'
> 
> 
> String 3 = site startswith 256
> 
> 
> 

Further more String 1 2 and 3 should split into

> 
> String operator = phoneNumer String operator = eq String operand =763436
> 
> 
> String operator = carrier String operator = eq String operand =AT and T Mobility
> 
> 
> String operator = site String operator = startswith String operand =256
> 
> 
> 

I can use string.split("and") and split(" ") for outer and inner respectively, but my string too contains whitespace and and also (example AT and T Mobility). One help is that extra and and white space will be present in operator only within quotes.

Any Help how to split in java?

Answer: 
Since `split` is regex-based, and if I'm reading the question correctly, you can just split for either one:

```
String[] split = yourString.split("(and)|\s+");

```

**Edit**:

Rather than splitting through regex for this, I would honestly recommend parsing the string yourself for something this specific:

```
public String[] parseRawString(String raw) {
    List<String> args = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    //whether or not to split on spaces
    boolean inQuotes = false;
    for (char c : raw.toCharArray()) {
        //if a quote is found
        if (c == '\'') {
            inQuotes = !inQuotes;
        //if a space is found outside quotes
        } else if (char == ' ' && !inQuotes) {
            args.add(sb.toString());
            sb.clear();
        //if a normal character is found or we're inside a quote
        } else if (char != ' ' || inQuotes) {
            sb.append(c);
        }
    }
    //add any last remnants that weren't added before the end
    if (!sb.isEmpty()) {
        args.add(sb.toString());
    }
    return args.toArray(new String[args.size()]);
}

```

This allows for some easier parsing:

```
String[] one = parseRawString("phoneNumber eq 763436");
/*
    one[0] = phoneNumber
    one[1] = eq
    one[2] = 763436
*/
String[] two = parseRawString("carrier eq 'AT and T Mobility'");
/*
    two[0] = carrier
    two[1] = eq
    two[2] = AT and T Mobility
*/
//etc...

```

That seems a bit closer to what you want, I believe.
Since you want those string as key,operand,value you can try this

```
    String regex="(?<key>\\S+)\\s+(?<operator>\\S+)\\s+(?<value>'[^']*'|\\S+)";
    for(String s:filter.split("\\s*and\\s*(?=([^']*'[^']*')*[^']*$)"))
    {
        Matcher m=Pattern.compile(regex).matcher(s);
        while(m.find())
        {
            System.out.println("Key:"+m.group("key"));
            System.out.println("operator:"+m.group("operator"));
            System.out.println("Value:"+m.group("value"));
        }
    }

```