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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
import javax.swing.*;
import java.awt.*;
import java.util.regex.*;
public class RegExp extends JFrame{
//Constructeur
public RegExp(){
setTitle("Expression rationnelles");
Dimension prefDim = new Dimension(800,600);
//setPreferredSize(prefDim);
setSize(800,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Conteneur principal
Container cont = getContentPane();
JPanel mainPanel = new JPanel();
cont.add(mainPanel);
mainPanel.setLayout(new BorderLayout());
Box bxVertical = Box.createVerticalBox();
//mainPanel.add(, BorderLayout.NORTH);
//mainPanel.add(, BorderLayout.SOUTH);
//mainPanel.add(, BorderLayout.EAST);
//mainPanel.add(, BorderLayout.WEST);
mainPanel.add(bxVertical, BorderLayout.CENTER);
// Configuration du Box Vertical
JTextArea exp = new JTextArea("exp"); // Zone de texte pour la RegExp.
bxVertical.add(exp);
Dimension dimexp = new Dimension(300,16);
exp.setMaximumSize(dimexp);
Box bxHorizontale = Box.createHorizontalBox();
bxVertical.add(bxHorizontale);
// Configuration de bxHorizontale
Box bxColGauche = Box.createVerticalBox();
bxHorizontale.add(bxColGauche);
// Configuration de bxColgauche
Box bxSelFic = Box.createHorizontalBox();
JTextArea src = new JTextArea("src"); // Zone de texte pour le texte source.
bxColGauche.add(bxSelFic);
bxColGauche.add(src);
// Configuration de bxSelFic
JTextField jtfSelFic = new JTextField(20);
JButton btnSelFic = new JButton("...");
bxSelFic.add(jtfSelFic);
bxSelFic.add(btnSelFic);
JTextArea res = new JTextArea("res"); // Zone de texte pour le résultat.
bxHorizontale.add(res);
//pack();
setVisible(true);
}
//Main
public static void main(String args[]){
//TODO
RegExp re = new RegExp();
// ([0-9]{2}-){2}[0-9]{4}
// (\d{2}-){2}\d{4}
//re.evalRegexp("\\Ga*b*","aaaaabaaaaabaaaaabaaaaabbaaaaabaaaaab aaaaabaaaaab");
re.evalRegexp("(\\d{2}-){2}\\d{4}","Ceci est une date : 16-11-2010 et en voici une autre 24-11-2010");
System.out.println("Fin du main");
}
// Pour le moment le type de sortie est mis à void mais la fonction
// devra clairement renvoyer un tableau dont le type sera probablement
// déterminé par le type renvoyé par matcher.
public void evalRegexp (String expr, String src){
Pattern motif;
Matcher crsp;
Boolean bIsRes;
motif = Pattern.compile(expr);
crsp = motif.matcher(src);
bIsRes = crsp.matches();
System.out.println(expr);
System.out.println(src);
System.out.println("Suspens !");
//*
if (bIsRes) {
System.out.println("Trouvé !");
for (int i=0; i <= crsp.groupCount(); i++){
// Remplissage d'un tableau mais pr le moment affichage
System.out.println(crsp.group(i));
}
}//DEBUG*/
while (crsp.find()){
System.out.println(crsp.group() +" Positions: ("+ crsp.start() + ", "+ crsp.end() + ")");
}
//return [tableau de sortie]
}
} |
Partager