Validate a Matcher

Asked

Viewed 74 times

1

Good afternoon.

I would like to know how to check whether the matcher found something. Because I am searching for information inside a PDF file and when it does not find anything I would like to have a condition that writes "Information not found"

while (matcherAut.find()) { //antes deste WHILE gostaria de ter um if e se o matcher não tiver encontrado nada escrever uma mensagem.
    Pattern pattern = Pattern.compile("\\s0.*|\\s1.*|\\s2.*|\\s5.*|\\s6.*|\\s7.*|\\s8.*|\\s9.*|^0.*|^1.*|^2.*|^5.*|^6.*|^7.*|^8.*|^9.*"); // Segundo Filtro (Elimina os que não começam com 3|4)
    Scanner scanner = new Scanner(matcherAut.group()).useDelimiter(pattern);
    Matcher matcher2 = pattern.matcher(aut);
    while (scanner.hasNext()) {
        aut = scanner.next();
        if (!linhasGravadas.contains(aut)) {
            System.out.println("Pagina: " + i);
            System.out.println("Autorização: " + matcherAut.group());
            /*System.out.println("Autorização: " + aut);
            gravarArq.println(aut);
            linhasGravadas.add(aut);*/
            Aut = Aut + 1;
        }
    }
}
  • This REGEX can be written as "(\\s|^)[12356789].*".

1 answer

1


You can try using the method find() or pouch() of Matcher, both return Boolean.

In your case:

if (!marcher2.find()) {
    System.out.println("Informação não encontrada");
}
  • 1

    Link-based responses are not recommended. Try to elaborate a response that is self-explanatory and contextualized, serving the link only for reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.