Gestion de caractéres joker

Contenu du snippet

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 / Exemple :


/*

  • 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

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (BScrk)