1
votes

Traitement de texte spécial en Java

J'ai un document avec du texte au format brut, exemple:

String s11 = test(1/2/3) for 11;
String s15 = test(1/2/3) for 15;
String s21 = test(1/2/3) for 21;
String s22 = test(1/2/3) for 22;
String s30 = test(1/2/3) for 30;
String s43 = test(1/2/3) for 43;
String s45 = test(1/2/3) for 45;
String s51 = test(1/2/3) for 51;
String s54 = test(1/2/3) for 54;
String s57 = test(1/2/3) for 57;
String s62 = test(1/2/3) for 62;
String s67 = test(1/2/3) for 67;
String s71 = test(1/2/3) for 71;
String s72 = test(1/2/3) for 72;
String s73 = test(1/2/3) for 73;
String s74 = test(1/2/3) for 74;
String s75 = test(1/2/3) for 75;
String s76 = test(1/2/3) for 76;
String s85 = test(1/2/3) for 85;
String s86 = test(1/2/3) for 86;
String s87 = test(1/2/3) for 87;

Je veux l'extraire en objet comme ci-dessous:

(11) test(1/2/3) for 11 (15) test(1/2/3) for 15 (21) test(1/2/3) for 21
(22) test(1/2/3) 
for 22
(30) test(1/2/3) for 30 (43) test(1/2/3) for 43
(45) test(1/2/3) 
for 45
(51) test(1/2/3) for 51 (54) test(1/2/3) for 54
(57) test(1/2/3) for 57
(62) test(1/2/3) for 62 (67) test(1/2/3) for 67
(71) test(1/2/3) for 71
(72) test(1/2/3) for 72 (73) test(1/2/3) for 73
(74) test(1/2/3) for 74
(75) test(1/2/3) for 75 (76) test(1/2/3) for 76
(85) test(1/2/3) for 85 (86) test(1/2/3) for 86
(87) test(1/2/3) for 87

Quelqu'un pourrait-il me donner un indice sur la façon de faire cela en Java?


1 commentaires

Désolé, je donne un autre exemple pour plus de détails, peut-être ci-dessus l'exemple pourrait conduire à un malentendu


3 Réponses :


3
votes

En supposant que vous puissiez tolérer la lecture du fichier entier dans une seule chaîne Java, alors le moteur d'expression régulière de Java a une manière propre de gérer ceci:

(11) test(1/2/3) for 11
(15) test(1/2/3) for 15
(21) test(1/2/3) for 21

Ceci imprime:

String input = "(11) test(1/2/3) for 11 (15) test(1/2/3) for 15 (21) test(1/2/3) for 21";
String pattern = "\\(\\d+\\) test\\(.*?\\) for \\d+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
List<String> lines = new ArrayList<>();
while (m.find()) {
    lines.add(m.group(0));
    System.out.println(m.group(0));
}

Notez qu'en général, vous ne souhaitez pas créer d'instances de chaîne distinctes pour chaque correspondance. Au lieu de cela, vous ajouteriez simplement toutes les correspondances à une collection, ou les traiteriez une par une à la volée lorsque vous les faites correspondre.


2 commentaires

Je mets à jour la question pour plus de détails. Dans votre réponse, la balise n'est pas corrigée.


Et j'ai rétrogradé votre question à la version originale, car votre modification invalide complètement ma réponse.



0
votes
String s11 = test(1/2/3) for 11
String s15 = test(1/2/3) for 15
String s21 = test(1/2/3) for 21

0 commentaires

0
votes

Supposons que votre document texte ressemble à ceci:

String s11 = test(1/2/3) for 11
String s15 = test(1/2/3) for 15
String s21 = test(1/2/3) for 21
String s22 = test(1/2/3) for 22
String s30 = test(1/2/3) for 30
String s43 = test(1/2/3) for 43
String s45 = test(1/2/3) for 45
String s51 = test(1/2/3) for 51
String s54 = test(1/2/3) for 54
String s57 = test(1/2/3) for 57
String s62 = test(1/2/3) for 62
String s67 = test(1/2/3) for 67
String s71 = test(1/2/3) for 71
String s72 = test(1/2/3) for 72
String s73 = test(1/2/3) for 73
String s74 = test(1/2/3) for 74
String s75 = test(1/2/3) for 75
String s76 = test(1/2/3) for 76
String s85 = test(1/2/3) for 85
String s86 = test(1/2/3) for 86
String s87 = test(1/2/3) for 87

Ensuite, vous pouvez faire ceci:

String filepath = "file.txt";
File file = new File(filepath);
Scanner sc = new Scanner(file);
String pattern = "\\(\\d+\\) test\\(.*?\\) for \\d+";
String input = sc.findInLine(pattern);
while(sc.hasNextLine()){
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(input);
    List<String> lines = new ArrayList<>();
    while (m.find()) {
        lines.add(m.group(0));
        //System.out.println(m.group(0));
        String[] split = m.group(0).split(" ");
        split[0] = split[0].replaceAll("\\p{P}","");
        System.out.println("String s"+split[0]+" = "+split[1] +" "+split[2]+" "+ split[3] );
    }
    sc.nextLine();
    input = sc.findInLine(pattern);
}
sc.close();

Le résultat:

(11) test(1/2/3) for 11 
(15) test(1/2/3) for 15 
(21) test(1/2/3) for 21
(22) test(1/2/3) for 22
(30) test(1/2/3) for 30 
(43) test(1/2/3) for 43
(45) test(1/2/3) for 45
(51) test(1/2/3) for 51 
(54) test(1/2/3) for 54
(57) test(1/2/3) for 57
(62) test(1/2/3) for 62 
(67) test(1/2/3) for 67
(71) test(1/2/3) for 71
(72) test(1/2/3) for 72 
(73) test(1/2/3) for 73
(74) test(1/2/3) for 74
(75) test(1/2/3) for 75 
(76) test(1/2/3) for 76
(85) test(1/2/3) for 85 
(86) test(1/2/3) for 86
(87) test(1/2/3) for 87


0 commentaires