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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public void testPattern(String aString)
{
// Match la matrice complete
Pattern pat = Pattern.compile("^\\((\\([0-9]+(,[0-9]+)*\\))(,\\([0-9]+(,[0-9]+)*\\))*\\)$");
Matcher match = pat.matcher(aString);
// Match ligne par ligne pour les compter
Pattern pat2 = Pattern.compile("\\([0-9]+(?:,[0-9]+)*\\)");
Matcher match2 = pat2.matcher(aString);
if (match.matches())
{
int start = 0;
int rows = 0;
int columns = 0;
boolean success = true;
while(match2.find(start))
{
rows++;
start = match2.end();
if (0 == columns)
{
columns = countColumn(match2.group());
}
else
{
if (columns != countColumn(match2.group()))
{
System.out.println("Matrice invalide, lignes non identiques en taille");
success = false;
}
}
}
if (success)
{
System.out.println("Trouve: lignes = " + rows + " Colonnes = " + columns);
}
}
else
{
System.out.println("No match");
}
}
private int countColumn(String aString)
{
int res = 1;
for (int ind=0; ind < aString.length(); ind++)
{
if (aString.charAt(ind) == ',')
{
res++;
}
}
return res;
} |
Partager