Problem parsing regular expressions of a jTextArea

0

I'm trying to parse text typed in a jTextArea, in the form of an algorithm in English. Initially I must recognize the main method, which is characterized by "beginning" and "end." The way I'm doing (code below), my logic only works if everything is on the same line, no matter what's between the two words for example:

start .... asdf end

I need that when typing in jTextArea, the logic works even if the line is broken by an Enter, for example:

home page ....
asdf
end

What can I do to work?

      String a = jTextArea1.getText();

    //----- METODO PRINCIPAL ----
    boolean valInicio = a.matches("^inicio.*");
    boolean valFim = a.matches(".*fim$");
    if (valInicio) {
        jTextArea2.setText(jTextArea2.getText() + "\nEcontrou o inicio do programa!");
        if (valFim) {
            jTextArea2.setText(jTextArea2.getText() + "\nEcontrou o fim do programa!");
        }
    }
    
asked by anonymous 31.10.2016 / 19:34

1 answer

1

The flags

String patternString = "[\s\S]*^inicio.*[\s\S]*fim$[\s\S]*";
Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);
System.out.println(pattern.matcher(a).find());

In this way, any text that begins with inicio , ends with fim and meets any of the criteria below will be found by regex:

  • Has line break (s) and / or space (s) before "start"
  • Has line break (s) and / or space (s) between "start" and "end"
  • Has line break (s) and / or space (s) after "end"
01.11.2016 / 14:52