Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

GESTION DE CARACTÉRES JOKER


Information sur la source

Catégorie :Divers Niveau : Initié Date de création : 28/01/2004 Date de mise à jour : 29/01/2004 14:28:10 Vu : 2 473

Note :
6 / 10 - par 1 personne
6,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

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
 

Commentaires et avis

signaler à un administrateur
Commentaire de capoueidiablo le 14/02/2006 23:03:36

c'est moi ou tu as recodé un truc qui existe deja ?

<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#matches(java.lang.String)">JavaDoc</a>

Ajouter un commentaire



Nos sponsors

Sondage...

CalendriCode

Décembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,218 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.