Accueil > > > GESTION DE CARACTÉRES JOKER
GESTION DE CARACTÉRES JOKER
Information sur la source
Description
Code qui teste si une String match avec une expression donnée contenant (ou non) certain caractéres JOKER ('*' et/ou '?') (genre les JOKER des commandes dos ou unix)
Source
- /*
- * Classe StringPattern:
- * teste si une String match avec une expression donnée contenant (ou non) certain
- * caractéres JOKER.
- * '*' : de 0 a n caractéres (n'importe lequel)
- * '?' : 1 caractére (n'importe lequel)
- *
- *
- * utilisation:
- * création d'une expression filtre
- * StringPattern tTest = new StringPattern(*EXPRESSION*);
- * exemple:
- * StringPattern tTest = new StringPattern("a?b*t");
- * puis application du filtre a une string tTest.matchs(**chaine a tester**)
- * exemple tTest.matchs("albert");
- *
- * ou testMatchsExpression(**chaine a tester**,*EXPRESSION*);
- *
- * @autor BScrk
- */
-
- class StringPattern
- {
-
- private String pattern;
-
- public StringPattern(String thePattern)
- {
- pattern = thePattern;
- }
-
- public StringPattern()
- {
- }
-
- public void setPattern(String thePattern) { pattern = thePattern; }
- public String getPattern() { return pattern; }
-
-
-
- public boolean testMatchsExpression(String word , String sPattern)
- {
- pattern = sPattern;
- return matchsR(word,sPattern);
- }
-
- public boolean matchs(String word)
- {
- return matchsR(word,pattern);
- }
-
-
-
- private boolean matchsR(String word , String sPattern)
- {
- //System.out.println(word+">"+sPattern);
- if (sPattern.length()==0) return (word.length()==0); //Base si sPattern est vide alors word doit etre vide
- if ((word.length()==0)) // si word est vide sPattern ne peut valoir que a * (1 a n fois)
- {
- if (sPattern.charAt(0)=='*')
- return matchsR(word,sPattern.substring(1,sPattern.length()));
- return false;
- }
- if ( ( word.charAt(0) == sPattern.charAt(0) ) || (sPattern.charAt(0) == '?') )
- { // Invariant 1 = word[i]=sPattern[i] ou sPattern[i]='?' avec i indice
- return matchsR(word.substring(1,word.length()),sPattern.substring(1,sPattern.length()));
- }
- else if (sPattern.charAt(0) == '*')
- {
- if (sPattern.length() == 1) return true; //toujours vrai pour sPattern = "*"
- if (sPattern.charAt(1)=='*')// elimination des * superflus
- return matchsR(word,sPattern.substring(1,sPattern.length()));
- else
- {
- int nbCar = (sPattern.substring(1,sPattern.length())).length();
- boolean test = false;
- String newPattern = sPattern.substring(1,sPattern.length());
- while ( nbCar <= word.length() && test == false )
- {
- test = matchsR(word,newPattern);
- newPattern = "?"+newPattern;
- nbCar++;
- }
- return test;
- }
- }
- return false; //sinon
- }
-
- /*public static void main(String args[]) {
-
- StringPattern tTest = new StringPattern();
- System.out.println(tTest.testMatchsExpression("testa","tes*a"));
- System.out.println(tTest.testMatchsExpression("tazeazetazeazet","t*?"));
- }*/
-
-
- }
-
-
-
/*
* Classe StringPattern:
* teste si une String match avec une expression donnée contenant (ou non) certain
* caractéres JOKER.
* '*' : de 0 a n caractéres (n'importe lequel)
* '?' : 1 caractére (n'importe lequel)
*
*
* utilisation:
* création d'une expression filtre
* StringPattern tTest = new StringPattern(*EXPRESSION*);
* exemple:
* StringPattern tTest = new StringPattern("a?b*t");
* puis application du filtre a une string tTest.matchs(**chaine a tester**)
* exemple tTest.matchs("albert");
*
* ou testMatchsExpression(**chaine a tester**,*EXPRESSION*);
*
* @autor BScrk
*/
class StringPattern
{
private String pattern;
public StringPattern(String thePattern)
{
pattern = thePattern;
}
public StringPattern()
{
}
public void setPattern(String thePattern) { pattern = thePattern; }
public String getPattern() { return pattern; }
public boolean testMatchsExpression(String word , String sPattern)
{
pattern = sPattern;
return matchsR(word,sPattern);
}
public boolean matchs(String word)
{
return matchsR(word,pattern);
}
private boolean matchsR(String word , String sPattern)
{
//System.out.println(word+">"+sPattern);
if (sPattern.length()==0) return (word.length()==0); //Base si sPattern est vide alors word doit etre vide
if ((word.length()==0)) // si word est vide sPattern ne peut valoir que a * (1 a n fois)
{
if (sPattern.charAt(0)=='*')
return matchsR(word,sPattern.substring(1,sPattern.length()));
return false;
}
if ( ( word.charAt(0) == sPattern.charAt(0) ) || (sPattern.charAt(0) == '?') )
{ // Invariant 1 = word[i]=sPattern[i] ou sPattern[i]='?' avec i indice
return matchsR(word.substring(1,word.length()),sPattern.substring(1,sPattern.length()));
}
else if (sPattern.charAt(0) == '*')
{
if (sPattern.length() == 1) return true; //toujours vrai pour sPattern = "*"
if (sPattern.charAt(1)=='*')// elimination des * superflus
return matchsR(word,sPattern.substring(1,sPattern.length()));
else
{
int nbCar = (sPattern.substring(1,sPattern.length())).length();
boolean test = false;
String newPattern = sPattern.substring(1,sPattern.length());
while ( nbCar <= word.length() && test == false )
{
test = matchsR(word,newPattern);
newPattern = "?"+newPattern;
nbCar++;
}
return test;
}
}
return false; //sinon
}
/*public static void main(String args[]) {
StringPattern tTest = new StringPattern();
System.out.println(tTest.testMatchsExpression("testa","tes*a"));
System.out.println(tTest.testMatchsExpression("tazeazetazeazet","t*?"));
}*/
}
Conclusion
PETITE MISE A JOUR effectuée le 29/01/04
Sources de la même categorie
LIRE LES FICHIERS .WAVLIRE LES FICHIERS .WAV Cette classe permet de lire les fichiers .wav, de les mettre en pause, et de les reprendre en cours de lecture sans bloquer l'OS....
par Julien39
TRADUCTEUR FRANÇAIS --> NERLANDAIS V3TRADUCTEUR FRANÇAIS --> NERLANDAIS V3C'est un traduction: Français - Néerlandais, Français - Anglais, Français - Japonais, et vice versa.
En fonction de la traduction demandé, les poss...
par edouard333
IA POUR DISCUTERIA POUR DISCUTER Ceci est une IA qui peut "parler" avec l'utilisateur, on a très peut de possibilité, mais aussi peut de chance de tomber dessus, et les dialogues sont...
par edouard333
JSUBTITLE1.0JSUBTITLE1.0Cette petite application permet d'avancer ou de faire reculer un sous titrage format srt.
Le titrage est chargé en memoire sous forme xml, l'enregist...
par darrylsite
COMPILATEUR PASCALCOMPILATEUR PASCAL c'est un mini compilateur pascal réalisé en java avec l'analyseur syntaxique et l'analyseur lexicale qui permet de compiler un fichier pascal...
par youma85
Commentaires et avis
|
Derniers Blogs
COMMENT MAPPER UNE VUE SQL SUR UNE COLLECTION DE COMPLEX TYPE?COMMENT MAPPER UNE VUE SQL SUR UNE COLLECTION DE COMPLEX TYPE? par Matthieu MEZIL
Avec EF, les vues doivent être mappées sur des entity types. Le problème c'est que les entity types doivent avoir une clé. Avec EF, nous avons les complex type qui n'ont pas de clé mais les vues ne peuvent pas être mappées dessus. Avec EF4, il est possibl...
Cliquez pour lire la suite de l'article par Matthieu MEZIL [WF4] UN BINDING ACTIVITY/ACTIVITYDESIGNER QUI PASSE MAL?[WF4] UN BINDING ACTIVITY/ACTIVITYDESIGNER QUI PASSE MAL? par JeremyJeanson
Certain d'entre vous on peut être vécu cette situation embarrassante après quelques temps passer avec WF4 : Au début avec mon " ActivityDesigner" , tout allait bien. Et puis un jour j'ai au des problèmes de " Binding" . Alors nous sommes allé sur le site ...
Cliquez pour lire la suite de l'article par JeremyJeanson MYTIC - SHAREPOINT 2010 : DéJà UN MYTHE MICROSOFT ?MYTIC - SHAREPOINT 2010 : DéJà UN MYTHE MICROSOFT ? par junarnoalg
La prochaine session de MyTIC aura lieu à Namur, le 23 mars prochain. Pendant presque une heure, nous parlerons de SharePoint 2010. Voici un aperçu du programme.
Accueil : 17h30 Début de la session : 18h00 - Les nouvelles int...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
PACKAGE ORACLEPACKAGE ORACLE par gaouinformaticien
Cliquez pour lire la suite par gaouinformaticien
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
Comparez les prix

HTC Magic
Entre 429€ et 429€
|