2009-11-20 5 views
0

Possible en double:
How to parse this output and separate each field/wordComment analyser du texte avec des délimiteurs?

Je veux analyser les données suivantes, de sorte que je reçois la sortie comme indiqué ci-dessous.

Entrée:

 
RTRV-ALM-EQPT::ALL:RA01; 

    SIMULATOR 09-11-20 13:52:15 
M RA01 COMPLD 
    "SLOT-1-1-1,CMP:MN,T-FANCURRENT-1-HIGH,NSA,01-10-09,00-00-00,,:\"Fan-T\"," 
    "SLOT-1-1-1,CMP:MJ,T-BATTERYPWR-2-LOW,NSA,01-10-09,00-00-00,,:\"Battery-T\"," 
    "SLOT-1-1-2,CMP:CR,PROC_FAIL,SA,09-11-20,13-51-55,,:\"Processor Failure\"," 
    "SLOT-1-1-3,OLC:MN,T-LASERCURR-1-HIGH,SA, 01-10-07,13-21-03,,:\"Laser-T\"," 
    "SLOT-1-1-3,OLC:MJ,T-LASERCURR-2-LOW,NSA, 01-10-02,21-32-11,,:\" Laser-T\"," 
    "SLOT-1-1-4,OLC:MN,T-LASERCURR-1-HIGH,SA,01-10-05,02-14-03,,:\"Laser-T\"," 
    "SLOT-1-1-4,OLC:MJ,T-LASERCURR-2-LOW,NSA,01-10-04,01-03-02,,:\"Laser-T\"," 
; 

sortie:

 
1) RTRV-ALM-EQPT::ALL:RA01; 
2) SIMULATOR 
3) 09-11-20 
4) 13:52:15 
5) M 
6) RA01 
7) COMPLD 
8) "SLOT-1-1-1,CMP:MN,T-FANCURRENT-1-HIGH,NSA,01-10-09,00-00-00,,:\"Fan-T\"," 
9) "SLOT-1-1-1,CMP:MJ,T-BATTERYPWR-2-LOW,NSA,01-10-09,00-00-00,,:\"Battery-T\"," 
10) "SLOT-1-1-2,CMP:CR,PROC_FAIL,SA,09-11-20,13-51-55,,:\"Processor Failure\"," 
11) "SLOT-1-1-3,OLC:MN,T-LASERCURR-1-HIGH,SA, 01-10-07,13-21-03,,:\"Laser-T\"," 
12) "SLOT-1-1-3,OLC:MJ,T-LASERCURR-2-LOW,NSA, 01-10-02,21-32-11,,:\" Laser-T\"," 
13) "SLOT-1-1-4,OLC:MN,T-LASERCURR-1-HIGH,SA,01-10-05,02-14-03,,:\"Laser-T\"," 
14) "SLOT-1-1-4,OLC:MJ,T-LASERCURR-2-LOW,NSA,01-10-04,01-03-02,,:\"Laser-T\"," 
+0

donc vous êtes sur des espaces autres que des espaces entre guillemets qui peuvent aussi contenir des guillemets? –

+0

Je pense que c'est la troisième fois que cette question est posée. Est-ce une sorte de devoir ou quoi? –

Répondre

0

Pour toute analyse syntaxique entrée, vous devez connaître sa structure.

  1. Les quatre premières lignes sont-elles toujours présentes?
  2. Quel est le format de chacune de ces quatre lignes?
1

La meilleure approche est probablement de ne pas penser à convertir le premier texte en second texte. Pensez plutôt à analyser d'abord le premier texte en un ensemble d'objets Java représentant ce qu'ils sont réellement. Par exemple, la deuxième/troisième ligne de votre entrée peut être représentée par une classe Test avec les propriétés "area", "day" et "time". (Seulement vous pouvez trouver un modèle raisonnable basé sur votre connaissance de ce que tout signifie).

Ensuite, une fois que vous avez une belle représentation en mémoire des informations du fichier, vous pouvez envisager d'imprimer sur du texte comme dans le second cas. Il devrait être très facile maintenant d'imprimer simplement les différents champs et propriétés de vos objets Java, plutôt que d'essayer de transformer le texte d'entrée à la volée.

1

En supposant que les fichiers sont relativement petits et peuvent donc être lus en mémoire. Essayez quelque chose comme ceci:

public class Main { 
    public static void main(String[] args) { 
     String text = "RTRV-ALM-EQPT::ALL:RA01;\n"+ 
      "\n"+ 
      " SIMULATOR 09-11-20 13:52:15\n"+ 
      "M RA01 COMPLD\n"+ 
      " \"SLOT-1-1-1,CMP:MN,T-FANCURRENT-1-HIGH,NSA,01-10-09,00-00-00,,:\\\"Fan-T\\\",\"\n"+ 
      " \"SLOT-1-1-1,CMP:MJ,T-BATTERYPWR-2-LOW,NSA,01-10-09,00-00-00,,:\\\"Battery-T\\\",\"\n"+ 
      " \"SLOT-1-1-2,CMP:CR,PROC_FAIL,SA,09-11-20,13-51-55,,:\\\"Processor Failure\\\",\"\n"+ 
      " \"SLOT-1-1-3,OLC:MN,T-LASERCURR-1-HIGH,SA, 01-10-07,13-21-03,,:\\\"Laser-T\\\",\"\n"+ 
      " \"SLOT-1-1-3,OLC:MJ,T-LASERCURR-2-LOW,NSA, 01-10-02,21-32-11,,:\\\" Laser-T\\\",\"\n"+ 
      " \"SLOT-1-1-4,OLC:MN,T-LASERCURR-1-HIGH,SA,01-10-05,02-14-03,,:\\\"Laser-T\\\",\"\n"+ 
      " \"SLOT-1-1-4,OLC:MJ,T-LASERCURR-2-LOW,NSA,01-10-04,01-03-02,,:\\\"Laser-T\\\",\"\n"+ 
      ";"; 
     Matcher m = Pattern.compile("\"(?:\\\\.|[^\\\"])*\"|\\S+").matcher(text); 
     int n = 0; 
     while(m.find()) { 
      System.out.println((++n)+") "+m.group()); 
     } 
    } 
} 

Sortie:

1) RTRV-ALM-EQPT::ALL:RA01; 
2) SIMULATOR 
3) 09-11-20 
4) 13:52:15 
5) M 
6) RA01 
7) COMPLD 
8) "SLOT-1-1-1,CMP:MN,T-FANCURRENT-1-HIGH,NSA,01-10-09,00-00-00,,:\"Fan-T\"," 
9) "SLOT-1-1-1,CMP:MJ,T-BATTERYPWR-2-LOW,NSA,01-10-09,00-00-00,,:\"Battery-T\"," 
10) "SLOT-1-1-2,CMP:CR,PROC_FAIL,SA,09-11-20,13-51-55,,:\"Processor Failure\"," 
11) "SLOT-1-1-3,OLC:MN,T-LASERCURR-1-HIGH,SA, 01-10-07,13-21-03,,:\"Laser-T\"," 
12) "SLOT-1-1-3,OLC:MJ,T-LASERCURR-2-LOW,NSA, 01-10-02,21-32-11,,:\" Laser-T\"," 
13) "SLOT-1-1-4,OLC:MN,T-LASERCURR-1-HIGH,SA,01-10-05,02-14-03,,:\"Laser-T\"," 
14) "SLOT-1-1-4,OLC:MJ,T-LASERCURR-2-LOW,NSA,01-10-04,01-03-02,,:\"Laser-T\"," 
15) ; 

La seule différence est qu'il ya un 15 match: le ;, que vous avez oublié, je crois.

La regex brute (sans toutes les évasions) ressemble à ceci:

"(?:\\.|[^\\"])*"|\S+ 

et matches:

"   # match a double quote 
(?:  # open non matching group 1 
    \\.  # match a backslash followed by any char (except line breaks) 
    |  # OR 
    [^\\"] # match any char except a backslash and a double quote 
)*   # close non matching group 1 and repeat it zero or more times 
"   # match a double quote 
|   # OR 
\S+  # match one or more characters other than white space chars 

En d'autres termes: correspondance une chaîne entre guillemets ou correspondant à un mot composé de seulement caractères non spatiaux.

+0

Grande réponse :) –

+0

Merci Andreas. –

Questions connexes