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
Commentaires et avis
|
Derniers Blogs
[FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : SQLRE : SQL par Julien39
Cliquez pour lire la suite par Julien39
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|