Probleme regex java

fezzani1985 Messages postés 2 Date d'inscription jeudi 19 août 2010 Statut Membre Dernière intervention 25 juin 2012 - 25 juin 2012 à 12:24
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 26 juin 2012 à 08:38
j ai besoin d afficher les numero de ligne d un commentaire et les commentaires voila un exemple
1 /*swing*/
2/*java*/


resultat que je veut :
1 swing
2 java

3 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
25 juin 2012 à 13:47
Salut,

Pour comprendre comment fonctionnent les regex : http://www.javafr.com/tutoriaux/UTILISATION-EXPRESSIONS-REGULIERES_1286.aspx

Dans ton cas, c'est assez simple :
1 /* swing*/ respecte la regexe suivante :
[0-9]+\\p{Space}*/\*\\p{Space}*.+\\p{Space}*\*/


Allez, je te fais la fonction :
public static void main(String[] args) {
String test="1 /*swing*/";
Matcher matcher = Pattern.compile("([0-9]+)\\p{Space}*/\\*\\p{Space}*(.+)\\p{Space}*\\*/").matcher(test);
while (matcher.find()) {
System.out.println(matcher.group(1)+" "+matcher.group(2));
}
}
0
fezzani1985 Messages postés 2 Date d'inscription jeudi 19 août 2010 Statut Membre Dernière intervention 25 juin 2012
25 juin 2012 à 15:23
ca marche pas
j ai besoin de parser un fichier et d afficher les commentaires et leurs numero de ligne , j ai reussie l extraire des commentaires mais les numero de ligne non
package Commentaire;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;



public class Essay extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

JList list ;
DefaultListModel model;
   final  JButton A ;
   JPanel panel ;
   JScrollPane P ;
   JCheckBox chek ;
public Essay() {
setSize(new Dimension(1000, 1000));
 list = new JList();
 list.setSize(new Dimension(700, 700));

model = new DefaultListModel();
panel = new JPanel() ;
    P = new JScrollPane() ;
A = new JButton("Afficher");
int i ;

A.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

try {

String filePath1 = "C:\\Users\\alaa.fezzani\\Desktop\\A.txt";
String line;
String data="blabla";


BufferedReader bf = new BufferedReader(new FileReader(filePath1));
try {
while ( (line = bf.readLine()) != null ) { 
      data += line;
     
}

String [] mesCommentaires = data.split("(/\\*)|(\\*/)");
for(int i = 1; i < mesCommentaires.length; i+=2){
       System.out.println(mesCommentaires[i]);
       list.setModel(model) ;
model.addElement(mesCommentaires[i]) ;

                     
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




}
});
P.getViewport().setView(list);
P.setPreferredSize(new Dimension(500, 500));
P.createVerticalScrollBar() ;
panel.add(A) ;
panel.add(P);

getContentPane().add(panel);
setVisible(true);

}


public static void main(String[] args) {

new Essay();

}


@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}

0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
26 juin 2012 à 08:38
Si, ce que je t'ai donné fonctionne, je l'ai testé !!!

resultat que je veut :
1 swing
2 java


Si tu ne veux pas du numéro de ligne :

	public static void main(String[] args) {
String test="1 /*swing*/";
Matcher matcher = Pattern.compile("[0-9]+\\p{Space}*/\\*\\p{Space}*(.+)\\p{Space}*\\*/").matcher(test);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
0
Rejoignez-nous