How to split a string by spaces, but avoid spaces inside quotes (in java)?

I have a line like this:

"Video or movie"    "parent"    "Media or entertainment"    "1" "1" "1" "0" "0"

I would like to break it into spaces, but the space inside the quote should be ignored. Thus, the split lines should be:

"Video or movie"
"parent"
"Media or entertainment"
"1"
...

Language is java.

+5
source share
5 answers

this should do your job:

   final String s = "\"Video or movie\"    \"parent\"    \"Media or entertainment\"    \"1\" \"1\" \"1\" \"0\" \"0\"";
        final String[] t = s.split("(?<=\") *(?=\")");
        for (final String x : t) {
            System.out.println(x);
        }

output:

"Video or movie"
"parent"
"Media or entertainment"
"1"
"1"
"1"
"0"
"0"
+6
source

You can use:

Patter pt = Pattern.compile("(\"[^\"]*\")");

Just keep in mind that this is also capturing ""(empty string).

TESTS:

String text="\"Video or movie\"    \"parent\"    \"Media or entertainment\"    \"1\" \"1\" \"1\" \"0\" \"0\"";
Matcher m = Pattern.compile("(\"[^\"]*\")").matcher(text);
while(m.find())
    System.out.printf("Macthed: [%s]%n", m.group(1));

CONCLUSION:

Macthed: ["Video or movie"]
Macthed: ["parent"]
Macthed: ["Media or entertainment"]
Macthed: ["1"]
Macthed: ["1"]
Macthed: ["1"]
Macthed: ["0"]
Macthed: ["0"]
+4
source

Instead of splitting, simply juxtapose things that are not space.

Pattern p = Pattern.compile("\"(?:[^\"\\\\]|\\\\.)*\"|\\S+");
Matcher m = p.matcher(inputString);
while (m.find()) {
  System.out.println(m.group(0));
}
+1
source

Divide by "[] +" instead? (including quotation marks)

You will probably need to add the missing "if they are not at the beginning or end of the line.

0
source

All Articles