Extraire des données d'un fichier texte en java

Résolu
javaint Messages postés 2 Date d'inscription jeudi 25 mai 2017 Statut Membre Dernière intervention 25 mai 2017 - 25 mai 2017 à 14:45
javaint Messages postés 2 Date d'inscription jeudi 25 mai 2017 Statut Membre Dernière intervention 25 mai 2017 - 25 mai 2017 à 15:55
Bonjour,
SVP j'ai besoin d'aide.
je veux extraire des données d'un fichier texte en java. voila un exemple d'une ligne de ce fichier:
likes:list of friends who like posts on the fb user wall L(userx) Likes[data=[NamedFacebookType[id=836615436419028 metadata=null name=Basma Youssfi type=null], NamedFacebookType[id=200283693703077 metadata=null name=Sahar Hmidet type=null], NamedFacebookType[id=1589600437936215 metadata=null name=Fifi Daly Omm Omar type=null], NamedFacebookType[id=2285678608326303 metadata=null name=Rà Nou type=null], NamedFacebookType[id=149760905397346 metadata=null name=Awatef Cherif type=null], NamedFacebookType[id=197868283998495 metadata=null name=Samar Maamouri type=null], NamedFacebookType[id=191663454652822 metadata=null name=Ben Ali Fatma Benali type=null], NamedFacebookType[id=1594576777456573 metadata=null name=Rania Trigui type=null], NamedFacebookType[id=10204633957434960 metadata=null name=Gasmi Ghassen type=null]] totalCount=0].

je veux juste extraire les nom souligner.
pouvez vous m'aider.
merci

1 réponse

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 127
25 mai 2017 à 15:22
Bonjour,

Tu peux faire ça avec des expressions régulières :

String line = "likes:list of friends who like posts ...";

Pattern pattern = Pattern.compile("name=([^=]+) type=");
for (Matcher matcher = pattern.matcher(line); matcher.find(); ) {
    System.out.println(matcher.group(1));
}
0
javaint Messages postés 2 Date d'inscription jeudi 25 mai 2017 Statut Membre Dernière intervention 25 mai 2017
25 mai 2017 à 15:55
merci bien KX. ça marche

if(ligne.contains("name"))
{
Pattern pattern = Pattern.compile("name=([^=]+) type=");
for (Matcher matcher = pattern.matcher(ligne+"\r\n");
matcher.find(); )
{
sortie.write(matcher.group(1)+"\r\n");
}
sortie.close();
}
0
Rejoignez-nous