import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^.*(?=\\nOUTRAS INFORMAÇÕES)";
final String string = "NOME: TESTE DE SILVA SAURO\n"
+ "CPF: 785.981.970-84\n"
+ "DECLARAÇÃO DE AJUSTE ANUAL\n"
+ "IMPOSTO SOBRE A RENDA - PESSOA FÍSICA\n"
+ "EXERCICIO 2018 ANO-CALENDÁRIO 2017\n"
+ "EVOLUÇÃO PATRIMONIAL\n"
+ "Bens e direitos em 31/12/2016\n"
+ "Bens e direitos em 31/12/2017\n"
+ "Dividas conus rcais em 31/12/2016\n"
+ "Divisas e ônus reais em 31/12/2017\n"
+ "100.580.873.91\n"
+ "100.329. 110,32\n"
+ "9135,456,07\n"
+ "8.571.962,06\n"
+ "OUTRAS INFORMAÇÕES\n"
+ "Rendimentos isentos e não tributáveis";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html