Bonsoir,

J'ai toujours du mal avec ces regexp, je veux juste parser le fameux fichier hosts, histoire d'afficher son contenu ...

Pour parser une ligne :
192.168.1.1 Test # Tests
Je fais une regexp pour l'adresseIP et une pour le nom de la machine et une pour la description :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
String regexpIP="\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
         String regexpNom="[^#\r\n]*" ;
         String regexpDescription="^#.*$";//^\s*#.*$;
En faisant recherche j'ai bien trouvé la bonne regexp pour l'IP, mais pour le reste, c'est plutôt plus simple, mais voilà ça bloque :/

en fait tout le code, si vous voulez :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int index = 0,end1=0,end2=0 ;
      Matcher matcher1=null,matcher2=null,matcher3 = null;
        // String regexp="\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b\\";
         String regexpIP="\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
         String regexpNom="[^#\r\n]*" ;
         String regexpDescription="^#.*$";//^\s*#.*$;
         Pattern SEPARATOR_PATTERN1 = Pattern.compile(regexpIP);
         Pattern SEPARATOR_PATTERN2 = Pattern.compile(regexpNom);
         Pattern SEPARATOR_PATTERN3 = Pattern.compile(regexpDescription);
 
         String Ligne[] = null; 
 
        try {
            Ligne=LireLigne.LireString("hosts");
 
 
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }       
       // if (matcher != null)matcher.reset(Ligne[index]);
    matcher1 = SEPARATOR_PATTERN1.matcher(Ligne[index]);        
        matcher2 = SEPARATOR_PATTERN2.matcher(Ligne[index]);
        matcher3 = SEPARATOR_PATTERN3.matcher(Ligne[index]);
 
 
 
 // Using find 
while(matcher1.find()) {
       String match1 = Ligne[index].substring(matcher1.start(),matcher1.end());            
       System.out.println("debut =>"+matcher1.start()+" match "+match1+" fin =>"+matcher1.end());
       end1=matcher1.end();
}
while(matcher2.find()) {
       String match2 = Ligne[index].substring(end1,matcher2.end());             
     //  System.out.println("debut =>"+end1+" match "+match2+" fin =>"+matcher2.end());
       end2=matcher2.end();
}
while(matcher3.find()) {
       String match3 = Ligne[index].substring(end2,matcher3.end());             
      System.out.println("debut =>"+end2+" match "+match3+" fin =>"+matcher3.end());
}