Regular Expression

Asked

Viewed 98 times

2

I have an application that reads a string and needs to detect parameters contained in the string. The parameter has the following pattern <<<.texto>>> (< and > are part of the parameter).

I was able to make an expression to capture the correct parameters. But it is also necessary to identify incorrect parameters (errors caused by user typing). Ex.: <texto>>>, <<Texto>>>, Texto>>>> and etc.

Expression for the correct parameters: (\\<{3}(\\w+)\\>{3})+

Someone can help me?

  • 3

    Wouldn’t it just be making one if !correto?

  • Yes, it can help in the assembly of the IF ?

  • See if Yure’s answer, I’d do something like that

  • Okay, thanks. I haven’t seen.

1 answer

4

You can do it this way:

public class Validator {

  public static boolean validarTexto(String texto) {
     Pattern p = Pattern.compile("\\<{3}(\\w+)\\>{3}");
     Matcher retorno = p.matcher(texto);
     return retorno.matches();
  }

}

Testing:

    System.out.println(Validator.validarTexto("<<<texto"));//false
    System.out.println(Validator.validarTexto("<<<texto2>>>"));//true
    System.out.println(Validator.validarTexto("<<<1texto>>"));//false
    System.out.println(Validator.validarTexto("<<<3texto2>>>"));//true

Test with if:

String seuTexto = "<<<texto";

if (Validator.validarTexto(seuTexto)) {
  System.out.println("Texto correto.");
} else {
  System.out.println("Texto inválido.");
}
  • Yure thanks for the help. But I still have one more question. I don’t know if I could explain what I wanted, but it’s because my string is a string that can contain more than one parameter, so this is how I would save invalid parameters?

  • Could you give an example with a string with more than one parameter.

  • String text = "Use <<<id>>> to perform import. Query <<<table>> searching for <<cicurito>>>"

  • Could someone help?

  • Managed to Resolve?

Browser other questions tagged

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