0

I would like to split a string by comma, but commas inside <p></p> should be ignored.

For example if I have the below String

" Test1, test2, <p> test3, test4, test5, </p> , test6, test7" 

I need the result to be :

Test1
Test2
<p> test3, test4, test5, </p>
test6 
test7

Using special character how can I achieve this ?

Zzrot
  • 285
  • 2
  • 3
  • 17
user2761483
  • 51
  • 2
  • 8
  • Can you elaborate question? – Shankar Shastri May 01 '17 at 14:34
  • Possible duplicate of [Java: splitting a comma-separated string but ignoring commas in quotes](http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes) – Tom May 01 '17 at 14:41

3 Answers3

1

You can split your String with this regex ,(?![^<p>]*</p>):

String str = "Test1, test2, <p> test3, test4, test5, </p> , test6, test7";
String spl[] = str.split(",(?![^<p>]*</p>)");

Output

Test1
test2
<p> test3, test4, test5, </p>
test6
test7

Demo here:

Rextester

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
YCF_L
  • 49,027
  • 13
  • 75
  • 115
  • 1
    I missed the splitting part :-) – Tim Biegeleisen May 01 '17 at 14:43
  • `[^

    ]` looks fishy. This is "anything that's not '>', '

    –  May 01 '17 at 14:54
  • @Arkadiy i think there are no way how you can represent

    except this way did you have any idea?

    – YCF_L May 01 '17 at 15:06
  • errm... `

    `? Some sort of nested negative lookahead? I am not the one answering :)

    –  May 01 '17 at 15:21
  • Here is what I came up with: `String spl[] = input.split("\\s*,\\s*(

    .*

    )?\\s*");` . Of course, it does not address the issue of nested tags, or capital `

    ` or `

    `. Please do not parse HTML with regex.
    –  May 01 '17 at 15:36
  • @Arkadiy it show me a wrong output check this https://ideone.com/ucYSql – YCF_L May 01 '17 at 15:40
  • 1
    Sure, it shows empty line because `,` is followed by another `,` , so it sees two separators next to each other. I'll leave fixing this as an exercise to teh reader. –  May 01 '17 at 15:51
0

You can first split using "p>": String[] temp = input.split("p>");

You will have three elements in temp in your example:

  • "Test1, test2, <"
  • "p> test3, test4, test5,
  • "p> , test6, test7"

Now in all strings in temp, replace '<', 'p>' and '

  • "Test1, test2,"
  • "p> test3, test4, test5,"
  • ", test6, test7"

Finally, split each line using ',' again.
Apart from some (easily removable) empty strings, it should work

Andrea Rossi
  • 731
  • 1
  • 6
  • 22
0

This should work, it's not the most optimal, sorry, but off the top of my head.

public static void main(String args[]) {
    String x = " Test1, test2, <p> test3, test4, test5, </p> , test6, test7";
    String newX[];
    newX = x.split(",");
    String newString = "";
    int j = 0;
    int count = 0;
    for(int i = 0; i < newX.length; i ++) {
        if(newX[i].contains("<p>")) {
            count ++;
            j=i;
            while(!newX[i].contains("</p>")) {
                newString = newString + "," + newX[i];
                newX[i] = "";
                i++;
            }
            newString = newString + "," + newX[i];
            newX[i] = "";
            newX[j] = newString;
            newX[j] = newX[j].replaceFirst(",", "");
        }
        else
            count++;
    }
    String output[] = new String[count];

    j = 0;
    for(int i = 0; i < newX.length; i++) {
        if(newX[i] != "" && j < count) {
            output[j] =  newX[i];
            System.out.println(output[j]);
            j++;
        }
    }
}
Zzrot
  • 285
  • 2
  • 3
  • 17