Detection d'une date

Résolu
nounouuuuu2010 Messages postés 18 Date d'inscription samedi 2 janvier 2010 Statut Membre Dernière intervention 7 décembre 2011 - 5 nov. 2011 à 23:01
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 7 nov. 2011 à 08:18
Bonsoir,
Je veux détecter la structure Date dans un texte.
j'ai essayé avec ce code
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.*;
public class TimeDetection {
// Converts the contents of a file into a CharSequence
// suitable for use by the regex package.
public static CharSequence fromFile(String filename) throws IOException {

FileInputStream fis = new FileInputStream(filename);

    FileChannel fc = fis.getChannel();

    // Create a read-only CharBuffer on the file
    ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
   
    return cbuf;
}
public static void main(String[] args) {
 
try {
    // Create matcher on file

    Pattern pattern = Pattern.compile("dd/MM/yyyy hh:mm:ss");
   
    Matcher matcher = pattern.matcher(fromFile("nb.txt"));
    
    
    // Find all matches
    while (matcher.find()) {
    	
        // Get the matching string
        String match = matcher.group();
       
    }
} catch (IOException e) {
}

}
}


avec le contenu du fichier
Event: Miami Heat vs. Orlando Magic
Venue: American Airlines Arena
Start: 11/3/2011 8:00:00 PM
Category: SPORTS BASKETBALL


mais aucun résultat pour le pattern recherché.
Pouvez-vous m'aider à résoudre le problème?
A voir également:

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
7 nov. 2011 à 08:18
Bonjour,

Pour cela, je te conseil de découper ton fichier avec le séparateur


String [] mots = chaine.split("
");

Tu obtiens alors un tableau avec tous les éléments.

Après, je ne sais pas quel est exactement ton problème, veux tu récupérer 11/3/2011 ou juste savoir si 11/3/2011 est dans la chaine.

Tu peux dans tous les cas utiliser une expression régulière contenant
"[0-9][0-9]?/[0-9][0-9]?/[0-9][0-9][0-9][0-9]"

Si tu ne connais pas les regex, je te conseil de jeter un œil à ceci : http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
3
Rejoignez-nous