Array com regex

5 respostas Resolvido
regexjava
G

Bom dia, pessoal.

Como faço para aplicar um regex em uma determinada palavra que esta em um vetor? Para que ele retorne true ou false
Exemplo abaixo:

String nome = "Gustavo Henrique"
			nome = nome.replace("\\s+"," ");
			String stringResultante = nome.trim();
			String[] palavra = stringResultante.split(stringResultante, ' ');
			String regexNome = "/[A-Za-zÀ-ú\\.\\,]{1,19}/";
			String regexSobrenome = "/[A-Za-zÀ-ú][A-Za-zÀ-ú]{2,19}/";

			if (palavra.length <= 1 || palavra == null) {
				return Var.valueOf(false);


				}
				else if (palavra.length <= 2 ) {

					if(!palavra[0, 1].matches(regexNome)){  
						return Var.valueOf(false);  
					}
					else  {
						return Var.valueOf(true);  
					}

				}

5 Respostas

S
if (array[posicao].matches(regex)) {
    // true
}
G
Mas teria como eu pegar duas posições?

if (array[0, 1].matches(regex)) {

// true

}
S

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
}
G

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);
				}
S
Solucao aceita
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));
}
Criado 10 de setembro de 2019
Ultima resposta 10 de set. de 2019
Respostas 5
Participantes 2