import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?=(?:\"\"\\s+?Controle\\s*Fiscal[\\w\\W]+?)?\"\"\\s+(?<NomeUc>[\\w\\W]+?)$(?:.*?)\"\"\\s+?(?<Cnpj>\\d{14})\\s*(?:.*?)(?<MesReferencia>\\d{2}\\/\\d{4})(?:.*?)(?<DtEmissao>\\d{2}\\/\\d{2}\\/\\d{4})(?:.*?)\\d{2}\\/\\d{2}\\/\\d{4}\\s+(?<NumFatura>\\d+)\\s+(?:.*?)(?<ExternalCode>\\d+-\\d+)\\s+(?:.*?)(?<DtVencimento>\\d{2}\\/\\d{2}\\/\\d{4})\\s+(?:.*?)(?<VlrTotalFatura>[0-9\\.]+,?\\d*)(?:.*?)Consta\\snesta\\sfatura\\s*R.\\s(?<VlrPisCofins>[0-9\\.]+,?\\d*)\\sreferentes\\sa\\s(?:.*?)(?<AliqPis>[0-9\\.]+,?\\d*)%\\sde\\sPIS\\se\\s(?:.*?)(?<AliqCofins>[0-9\\.]+,?\\d*)%\\sde\\sCOFINS(?:.*?)\\)\\s+\\d+\\s+(?:\\d+)?\\s+(?<VlrBaseIcms>[0-9\\.]+,?\\d*)(?:.*?)\"\"\\s+(?<AliqIcms>\\d+)\\s+(?:.*?)^(?<DContratadaFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)[0-9\\.]+,?\\d*\\s+(?<VlrIcms>[0-9\\.]+,?\\d*)\\s+(?:.*?)\"\"\\s+(?<DataLeituraAtual>\\d{2}\\/\\d{2}\\/\\d{4})\\s+(?:.*?)(?<DataLeituraAnterior>\\d{2}\\/\\d{2}\\/\\d{4})\\s+(?:.*?)\"\"\\s+(?<LeituraAtualCFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAnteriorCFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<ConstanteConsumoFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<CMedidoFp>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAtualCP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<LeituraAnteriorCP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<ConstanteConsumoP>[0-9\\.]+,?\\d*)\\s+(?:.*?)(?<CMedidoP>[0-9\\.]+,?\\d*)(?:.*?))";
final String string = "\"\" Controle Fiscal: 0B3EADFCEE1EECF867DEBB24AB24C691\n"
+ "\"\" AGUAS DE NITEROI S/A \n"
+ "\"\" ROD BR 101 KM 30,5, S/N DISTRITO CONSELHEIRO \n"
+ "\"\" 02150336000166 0704097\n"
+ "\"\" 86 41570091 0108-8\n"
+ "\"\" FAZ NOVA GAIVOT 12/2019\n"
+ "\"\" CAMPOS DOS GOYTACAZES 20/12/2019\n"
+ "\"\" 28100-000 24/12/2019\n"
+ "4421 Segunda Via\n"
+ "7889020-9 16/01/2020 2.447,37\n"
+ "Cons Fat Ponta 814,00 kWh2,781802.264,38\n"
+ "Cons Fat F Ponta 8568,00 kWh 0,541594.640,34\n"
+ "Dem Fat F Ponta 73,00 kW31,790002.320,67\n"
+ "Ajuste -4.833,30\n"
+ "Iluminacao Publica 126,69\n"
+ "ENERGIA CONSUMIDA GD BAND VERDE 6.015,70\n"
+ "Cred Energia Injetada G. Distribuida -6.015,70 0,00\n"
+ "Devolucao ICMS -2.071,41\n"
+ "\"\" 0,00\n"
+ "VR ADIC BAND AMARELA DO MES ( R$ 347,52 ) \n"
+ "Consta nesta fatura R$ 533,22 referentes a 1,04% de PIS e 4,74% de COFINS \n"
+ "(Art. 10 Res. 298/2006-ANEEL e Leis N. 10.637/02 e 10.883/03) \n"
+ "33375978 9.225,39 \n"
+ "\"\" 30 \n"
+ "1.000,00 0,00 2.767,61 \n"
+ "Tipo de Faturamento: Normal \n"
+ "19/12/2019 19/11/2019 19/12/2019 19/11/2019\n"
+ "357,00 255,00 84,000 8568,00 3555,00 2586,00 0,840 814,00\n"
+ "63,00 41,00 3,360 73,00 46,00 30,00 3,360 53,00\n"
+ "1,00 1,00 84,000 0,00 0,00 0,00 0,840 0,00\n"
+ "219,00 140,00 0,840 66,36 137,00 89,00 0,840 40,32\n"
+ "\"\" 13,80 kV IGOR MACHADO\n"
+ "\"\" 20/01/2020\n"
+ "0 0 0 0 0 THS VERDE A4 C/DES15Telefone:2716-1634\n"
+ "0 0 0 0 14,49 kV\n"
+ "0 0 E-mail:igor.santos@enel.com10/2019 Servico Publico\n"
+ "\"\" 12,83 kV 0,00%\n"
+ "0 0 \n"
+ "Banco Itaú SA 341-7 34191.09008 30346.832931 83707.520009 1 81360000244737 \n"
+ "PAGÁVEL PREFERENCIALMENTE NO ITAÚ Contra-apresentação\n"
+ "AMPLA Energia e Serviços S.A. 2938/37075-2\n"
+ "20/12/2019 4421 DP N 175/00303468-3\n"
+ "\"\" 175 2.447,37\n"
+ "Pagamentos efetuados após o dia 16/01/2020, incorrerão encargos financeiros de mora. \n"
+ "Os encargos financeiros citados, serão cobrados em faturas posteriores. \n"
+ "Regime Especial - processo no E-04/142.091/97 \n"
+ "AGUAS DE NITEROI S/A \n"
+ "ROD BR 101 KM 30,5, S/N DISTRITO CONSELHEIRO \n"
+ "\"\" CONTA PAGA \n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE);
final Matcher matcher = pattern.matcher(string);
if (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