Bonsoir tout le monde,
J'ai une petite question concernant les regex sur les chaines de caratères.
Prenons cet exemple :
Cela va me renvoyer :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
29
30
31
32
33 public class TestRegex { public static void Matcher(String mot, String motTableau){ Pattern pattern; Matcher matcher; pattern = Pattern.compile(mot); matcher = pattern.matcher(motTableau); if(matcher.find()){ System.out.println(mot+" : "+motTableau); } } public static void main(String[] args){ String [] tableau = new String[3]; tableau[0] = "maison"; tableau[1] = "le"; tableau[2] = "elle"; String mot = "maisons"; String mot2 = "elle"; for(int i =0; i < tableau.length; i++) { String motTableau = tableau[i]; Matcher(mot,motTableau); } for(int i =0; i < tableau.length; i++) { String motTableau = tableau[i]; Matcher(mot2,motTableau); } } }
elle : elle
et la String "maisons" ne sera pas trouvée.
Si on inverse mot et motTableau dans la procédure Matcher
public static void Matcher(String motTableau, String mot)
le résultat sera :
maison : maisons
le : elle
elle : elle
La String "maisons" est trouvée comme on le souhaite, la String "elle" est également trouvée. Cependant "elle" a aussi matcher sur "le".
Comment peut-on faire pour que "elle" ne match pas sur "le" dans cette situation ?
Partager