venkat
venkat

Reputation: 41

Split string by tab and ignore tab in multi double quotes

public static void main(String[] args) {
    String text = "hi   ravi    \"how   are you\"   when    are you coming";
    String regex = "\"([^\"]*)\"|(\\S+)";

    Matcher m = Pattern.compile(regex).matcher(text);
    while (m.find()) {
        if (m.group(1) != null) {
            System.out.println("Quoted [" + m.group(1) + "]");
        } else{
            System.out.println("Plain [" + m.group(0) + "]");
        }
    }
    // getSplits(text);
}

Output:

Plain [hi]
Plain [ravi]
Quoted [how are you]
Plain [when]
Plain [are]
Plain [you]
Plain [coming]

Above code is working fine if the given text has only one single quotation. Can any one help me how to get below output with below input:

text = "hi  ravi    \"\"how are\"   you\"   when    are you coming";

Expected Output:

Plain [hi]
Plain [ravi]
Quoted ["how are" you]
Plain [when]
Plain [are]
Plain [you]
Plain [coming]

Upvotes: 2

Views: 224

Answers (2)

Ruben Pirotte
Ruben Pirotte

Reputation: 386

Following regex works for your example input/output. You will have to give a more detailed description of the expected result, as this might not be what you were expecting.

public static void main(String[] args) {
    String text = "hi  ravi    \"\"how are\"   you\"   when    are you coming";
    String regex = "(\".+\")|(\\S+)";

    Matcher m = Pattern.compile(regex).matcher(text);

    while (m.find()) {
        if (m.group(1) != null) {
            System.out.println("Quoted [" + m.group(1) + "]");
        } else{
            System.out.println("Plain [" + m.group(0) + "]");
        }
    }
    // getSplits(text);
}

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34170

This will do:

[\t]+(?=([^"]*"[^"]*")*[^"]*$)

See the DEMO

Upvotes: 0

Related Questions