Extraire chaine de caractère
khalous91
Messages postés
18
Date d'inscription
Statut
Membre
Dernière intervention
-
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour
Alors voilà j'ai un petit problème : Voici mon code ci dessous :
code :
en sortie j'ai :
0 : #----------------------------------------------------------------------
1 : #{toto}# Variables
2 : #----------------------------------------------------------------------
3 : #> should be formatted by calmod_python
4 : set conf_etudie = "{etude}_{SIT}_sylda_{h_sylda}"
5 : set dir_listing = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/listing"
6 : set dir_tem = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/temoin"
7 : set dir_res = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/results"
8 : set dir_UCI_cree = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/UCI_cree"
9 : #< end the formatted line
10 : set fic_tem = calmod.tem
11 : #----------------------------------------------------------------------
toto
etude
SIT
h_sylda
userPath
conf_cu
logName
horodate
Found Index :163
Found Index :707
Mon but c'est de ne plus avoir toto en sortie car il ny a pas pas le #> devant lui.
Que me conseillez vous ?
EDIT: Précision du langage dans la coloration syntaxique.
Alors voilà j'ai un petit problème : Voici mon code ci dessous :
code :
import java.util.Collection; import java.util.LinkedHashSet; public class Extract { public static void main(String[] args) { String zoneB = "#>"; String zoneE = "#<"; String valueB = "{"; String valueE = "}"; Collection<String> mots = new LinkedHashSet<>(); // si on voulait avoir tous les mots : Collection<String> mots = new ArrayList<>(); String string = "#----------------------------------------------------------------------\n" + "#{toto}"+ "# Variables\n" + "#----------------------------------------------------------------------\n" + "#> should be formatted by calmod_python\n" + "set conf_etudie = \"{etude}_{SIT}_sylda_{h_sylda}\"\n" + "set dir_listing = \"{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/listing\"\n" + "set dir_tem = \"{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/temoin\"\n" + "set dir_res = \"{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/results\"\n" + "set dir_UCI_cree = \"{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/UCI_cree\"\n" + "#< end the formatted line\n" + "set fic_tem = calmod.tem\n" + "#----------------------------------------------------------------------\n"; String[] tab = string.split("\n").clone(); for ( int i = 0; i < tab.length; i++ ) { // on pourrait faire comme ça aussi : for(String line : tab ) { System.out.println(i + " : " + tab[i]); int index1 = tab[i].indexOf("{"); // nous donne l'emplacement du premier "{" while( index1>=0 ) { // tant qu'on trouve une { int index2 = tab[i].indexOf("}", index1); // on cherche le } qui suit if ( index2>= 0 ) { // si on trouve un } // on pourrait tester ici un cas d'erreur si on a 2 { qui se suivent sans } entre : int index3 = tab[i].lastIndexOf("{", index2); // on cherche le { qui précède le } if ( index3!=index1 ) { // si ce n'est pas le même que index1, on a forcément { ... { ... } System.err.println("Problème dans le fichier : { non fermée en " + index1 ); } String x = tab[i].substring(index1+1, index2); // on extrait le mot trouvé entre { et } // ICI CORRECTION tab[i] à la place de yes et le +1 que j'ai oublié // System.out.println("x = " + x); // et on l'affiche (ou on le stocke dans une ArrayList pour l'utiliser ailleurs...) mots.add( x ); // on stocke le mot } else { // on a trouvé un { mais pas de } après : c'est probablement une erreur System.err.println("Problème dans le fichier : { non fermée en " + index1); break; // on sort de la boucle, parce que ce n'est pas la peine de continuer de chercher } index1 = tab[i].indexOf("{", index2 ); // on cherche le mot suivant dans la ligne } // fin de la boucle while } // fin de la boucle for for(String current : mots) // pour chaque String current dans mots { System.out.println(current); // on affiche current } // pour trouver les positions #> et #< System.out.print("Found Index :" ); System.out.println(string.indexOf( "#>" )); System.out.print("Found Index :" ); System.out.println(string.indexOf( "#<" )); // pour extraire la chaîne entre la position premier incluse et la position dernier exclue. int a = string.indexOf("#>"); int b = string.indexOf("#<"); } }
en sortie j'ai :
0 : #----------------------------------------------------------------------
1 : #{toto}# Variables
2 : #----------------------------------------------------------------------
3 : #> should be formatted by calmod_python
4 : set conf_etudie = "{etude}_{SIT}_sylda_{h_sylda}"
5 : set dir_listing = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/listing"
6 : set dir_tem = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/temoin"
7 : set dir_res = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/results"
8 : set dir_UCI_cree = "{userPath}/{etude}/script/{etude}_{SIT}_sylda_{h_sylda}/{conf_cu}/{logName}/{horodate}/UCI_cree"
9 : #< end the formatted line
10 : set fic_tem = calmod.tem
11 : #----------------------------------------------------------------------
toto
etude
SIT
h_sylda
userPath
conf_cu
logName
horodate
Found Index :163
Found Index :707
Mon but c'est de ne plus avoir toto en sortie car il ny a pas pas le #> devant lui.
Que me conseillez vous ?
EDIT: Précision du langage dans la coloration syntaxique.
A voir également:
- Extraire chaine de caractère
- Caractère ascii - Guide
- Extraire une video youtube - Guide
- Caractère spéciaux - Guide
- Caractere speciaux - Guide
- Extraire le son d'une vidéo - Guide
1 réponse
Bonjour,
Il me semble qu'un simple test
Remarque : en Java on peut manipuler des expressions régulières, ce qui rend ton problème très simple.
Il me semble qu'un simple test
if (tab[i].startsWith("#"))suffit
Remarque : en Java on peut manipuler des expressions régulières, ce qui rend ton problème très simple.
final Pattern PATTERN = Pattern.compile("\\{([^\\}]*)\\}"); Set<String> set = new LinkedHashSet<String>(); for (String line : string.split("\n")) { if (line.startsWith("#")) continue; Matcher matcher = PATTERN.matcher(line); while (matcher.find()) set.add(matcher.group(1)); } System.out.println(set);