Recherche de mots correspondants à un pattern
Bonjour !
Ma demande concerne simplement l'obtention d'une idée concernant la recherche de mots présents dans un tableau de Strings, mots correspondants à un pattern.
Par exemple, le pattern
"?A???N"
accepte les mots "MAISON" et SAISON", mais pas "ZIRCON".
L'embryon de code ci-après fonctionne, mais est fort primaire.
Existe-t-il un moyen plus évolué de réaliser cette recherche ?
Merci pour votre aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Main {
public static void main(String[] args) {
String pattern = "?A???N";
String[] words = new String[]{
"MAISON", "RAISON", "CUIVRE",
"BAGDES", "ZIRCON", "SAISON" };
boolean status = false;
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < pattern.length(); j++) {
if (pattern.charAt(j) == '?') {
continue;
} else if (pattern.charAt(j) == words[i].charAt(j)) {
status = true;
} else {
status = false;
break;
}
}
if (status == true) System.out.println(words[i]);
}
}
}
// Affiche :
MAISON
RAISON
SAISON |