6

I have a string like this:

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

I would like to split it by the spaces but the space inside the quote should be ignored. So the splitted strings should be:

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

The language is java.

user3111525
  • 5,013
  • 9
  • 39
  • 64

5 Answers5

6

this should do the job for you:

   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"
Kent
  • 189,393
  • 32
  • 233
  • 301
4

You can use:

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

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

TESTING:

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));

OUTPUT:

Macthed: ["Video or movie"]
Macthed: ["parent"]
Macthed: ["Media or entertainment"]
Macthed: ["1"]
Macthed: ["1"]
Macthed: ["1"]
Macthed: ["0"]
Macthed: ["0"]
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Take a look at this question. You may be able to adapt its solution to ignore the spaces in the quotes rather than the commas.

Java: splitting a comma-separated string but ignoring commas in quotes

Community
  • 1
  • 1
tomcheney
  • 1,750
  • 1
  • 17
  • 33
1

Instead of splitting, just match things that are not space.

Pattern p = Pattern.compile("\"(?:[^\"\\\\]|\\\\.)*\"|\\S+");
Matcher m = p.matcher(inputString);
while (m.find()) {
  System.out.println(m.group(0));
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

Split by "[ ]+" instead? (including the quotes)

You're likely to need to add in missing "'s if they aren't at the start or end of the string.

Dan Hardiker
  • 3,013
  • 16
  • 19