begin process at 2012 02 09 07:26:08
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > GESTION DE CARACTÉRES JOKER

GESTION DE CARACTÉRES JOKER


 Information sur la source

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

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

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

Auteur : BScrk

Ecrire un message privé
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


 Sources du même auteur

Source avec Zip JEU DE MARELLE

 Sources de la même categorie

Source avec Zip Source avec une capture COMPLÉTION AUTOMATIQUE par Julien39
Source avec Zip Source avec une capture [JOGL] ANIMATION 3D DANS UN BOUTON par loloof64
Source avec Zip CALCULATRICE BIEN FAITE (VERSION SIMPLIFIÉE) par Julien39
Source avec une capture YNOT, UN NOUVEAU LANGAGE DE SCRIPT ÉCRIT EN JAVA. par apmneo7114
Source avec Zip KIT DE FICHIERS DE PROGRAMMATION par edouard333

Commentaires et avis

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

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), 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

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 3,307 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales