2012-01-20 9 views

Odpowiedz

6

powinno to zrobić dla ciebie zadanie.

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

wyjście.

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

To jest to, czego potrzebowałem! – user3111525

0

Podział przez "[] +" zamiast? (W tym cudzysłowów)

jesteś prawdopodobnie trzeba dodać brakujące „'s, jeśli nie są one na początku lub na końcu łańcucha

1

Zamiast podziału, po prostu pasuje do rzeczy, które nie mają miejsca

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

Można użyć:

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

Wystarczy pamiętać, że to również przechwytywać "" (pusty ciąg znaków).

BADANIA:

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

WYJŚCIE: "` się uciec w swoim scenariuszu `"

Macthed: ["Video or movie"] 
Macthed: ["parent"] 
Macthed: ["Media or entertainment"] 
Macthed: ["1"] 
Macthed: ["1"] 
Macthed: ["1"] 
Macthed: ["0"] 
Macthed: ["0"] 
Powiązane problemy