staroski
if (array[posicao].matches(regex)) {
// true
}
Gustavo_Henrique2
Mas teria como eu pegar duas posições?
if (array[0, 1].matches(regex)) {
// true
}
staroski
Arrays só podem ser acessados um índice por vez, ainda não há uma sintaxe para acessar um intervalo de valores.
Você pode utilizar um laço e/ou a operação lógica E:
// se a posição 0 E a posição 1 atendem à regex, faz algo
if (array[0].matches(regex) && array[1].matches(regex)) {
// true
}
Gustavo_Henrique2
Como faria com for? pois fiz dessa forma, mas ficou muito extenso.
nome = nome.replace("\\s+", " ");
String stringResultante = nome.trim();
String[] palavra = stringResultante.split(" ");
String regexNome = "[A-Za-zÀ-ú]{3,19}";
String regexSobrenome = "[A-Za-zÀ-ú\\.\\,]{2,19}";
if (palavra.length <= 1 || palavra == null) {
return Var.valueOf(false);
} else if (palavra.length == 2) {
if ((!palavra[0].matches(regexNome)) || (!palavra[1].matches(regexNome))) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else if (palavra.length == 3) {
if (!palavra[0].matches(regexNome) || !palavra[1].matches(regexSobrenome) || !palavra[2].matches(regexNome)) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else if (palavra.length == 4) {
if (!palavra[0].matches(regexNome) || !palavra[1].matches(regexSobrenome) || !palavra[2].matches(regexSobrenome) ||
!palavra[3].matches(regexNome)) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else if (palavra.length == 5) {
if (!palavra[0].matches(regexNome) || !palavra[1].matches(regexSobrenome) || !palavra[2].matches(regexSobrenome) ||
!palavra[3].matches(regexSobrenome) || !palavra[4].matches(regexNome)) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else if (palavra.length == 6) {
if (!palavra[0].matches(regexNome) || !palavra[1].matches(regexSobrenome) || !palavra[2].matches(regexSobrenome) ||
!palavra[3].matches(regexSobrenome) || !palavra[4].matches(regexSobrenome) || !palavra[5].matches(regexNome)) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else if (palavra.length == 7) {
if (!palavra[0].matches(regexNome) || !palavra[1].matches(regexSobrenome) || !palavra[2].matches(regexSobrenome) ||
!palavra[3].matches(regexSobrenome) || !palavra[4].matches(regexSobrenome) || !palavra[5].matches(regexSobrenome) || !palavra[6].matches(regexNome)) {
return Var.valueOf(false);
} else {
return Var.valueOf(true);
}
} else {
return Var.valueOf(false);
}
Solucao aceita
staroski 1 like
private boolean nomeValido(String nomeCompleto) {
if (nomeCompleto == null) {
return false;
}
String[] partes = nomeCompleto.trim().split("\\s");
if (partes.length < 2) {
return false;
}
String regexNome = "[A-Za-zÀ-ú]{3,19}";
if (!partes[0].matches(regexNome)) {
return false;
}
String regexSobrenome = "[A-Za-zÀ-ú\\.\\,]{2,19}";
for (int i = 1; i < partes.length; i++) {
if (!partes[i].matches(regexSobrenome)) {
return false;
}
}
return true;
}
Exemplo de uso:
String[] nomesParaTestar = new String[] {
"Pedro",
"Maria",
"João da Silva",
"José Correa",
"Francisco dos Santos",
"Ana Paula"
};
for (String nome : nomesParaTestar) {
System.out.printf("%s: %s%n", nome, nomeValido(nome));
}